Hi everyone,
I’m woking on a project that needs to send data throught web services, i’m using dpws on a ChipworkX as client, on the server side is a Windows 7 PC. The basic idea is to bring send some data from the pc to update a database on the CWX, the problem arises when the service is exposed on the IIS, the device is able to discover the service but when is sending the request the process stops and shows and error saying that the host is not available.
I already done some test shutting down the firewall and giving access on the router but the problem continues. But when deploy the service on a self hosting environment (console) i have no problems, then my question is why it works that way and not when is published on the IIS?
This is the code that im using:
For the server,
namespace ServiceHelloWCF
{
[DataContract(Namespace = "http://192.168.0.101/InfoService")]
public class EmployeeInfo
{
private string eName;
private int eId;
//private byte[] jpegImage;
[DataMember]
public string Name
{
get { return eName; }
set { eName = value; }
}
[DataMember]
public int ID
{
get { return eId; }
set { eId = value; }
}
}
}
namespace ServiceHelloWCF
{
[ServiceBehavior(Namespace = "http://192.168.0.101/InfoService")]
public class InfoService : IInfoService
{
EmployeeInfo employee = new EmployeeInfo();
public EmployeeInfo GetEmployeeInfo()
{
employee.Name = "Snake";
employee.ID = 666;
return employee;
}
}
}
namespace ServiceHelloWCF
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract(Namespace = "http://192.168.0.101/InfoService")]
public interface IInfoService
{
[OperationContract]
EmployeeInfo GetEmployeeInfo();
}
}
namespace ServiceHelloWCF
{
class Program
{
static void Main(string[] args)
{
IPHostEntry entry = Dns.GetHostEntry("");
string addr = "192.168.0.101";
/*for (int i = 0; i < entry.AddressList.Length; i++)
{
if (entry.AddressList[i].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
addr = entry.AddressList[i].ToString();
break;
}
}*/
Uri baseAddress = new Uri("http://" + addr + ":8090/InfoService");
ServiceHost serviceHost = new ServiceHost(typeof(InfoService), baseAddress);
try
{
ServiceEndpoint wsEndpoint = serviceHost.AddServiceEndpoint(typeof(IInfoService), new WSHttpBinding(SecurityMode.None), string.Empty);
EndpointDiscoveryBehavior endpointDiscoveryBehavior = new EndpointDiscoveryBehavior();
// Add the discovery behavior to the endpoint.
wsEndpoint.Behaviors.Add(endpointDiscoveryBehavior);
// Make the service discoverable over UDP multicast
serviceHost.Description.Behaviors.Add(new ServiceDiscoveryBehavior());
serviceHost.AddServiceEndpoint(new UdpDiscoveryEndpoint(DiscoveryVersion.WSDiscovery11));
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.HttpGetUrl = baseAddress;
serviceHost.Description.Behaviors.Add(smb);
serviceHost.Open();
Console.WriteLine("Hello World WCF Service started at {0}", baseAddress);
Console.WriteLine();
Console.WriteLine("Press <ENTER> to terminate the service.");
Console.WriteLine();
Console.ReadLine();
serviceHost.Close();
}
catch (CommunicationException e)
{
Console.WriteLine(e.Message);
}
catch (TimeoutException e)
{
Console.WriteLine(e.Message);
}
if (serviceHost.State != CommunicationState.Closed)
{
Console.WriteLine("Aborting service...");
serviceHost.Abort();
}
}
}
}
On the Client (CWX)
namespace OvasysImageClient
{
public class ClientApp
{
IInfoServiceClientProxy m_clientProxy;
public GetEmployeeInfoResponse response;
private bool Discover(IInfoServiceClientProxy proxy)
{
DpwsServiceTypes typeProbes = new DpwsServiceTypes();
typeProbes.Add(new DpwsServiceType("InfoService", "http://192.168.0.101/InfoService"));
DpwsServiceDescriptions descriptions = proxy.DiscoveryClient.Probe(typeProbes, 1, 20000);
if (descriptions.Count > 0)
{
proxy.EndpointAddress = descriptions[0].Endpoint.Address.AbsoluteUri;
return true;
}
return false;
}
public void Run()
{
Uri remoteEp = new Uri("http://192.168.0.101:8090/InfoService");
HttpTransportBindingConfig tbc = new HttpTransportBindingConfig(remoteEp);
HttpTransportBindingElement el = new HttpTransportBindingElement(tbc);
WS2007HttpBinding binding = new WS2007HttpBinding(tbc);
ProtocolVersion ver = new ProtocolVersion11();
m_clientProxy = new IInfoServiceClientProxy(binding, ver);
m_clientProxy.IgnoreRequestFromThisIP = false;
if (!Discover(m_clientProxy))
{
Debug.Print("Error");
m_clientProxy.EndpointAddress = "http://192.168.0.101:8090/InfoService";
}
GetEmployeeInfo req = new GetEmployeeInfo();
//req.message = "death.jpg";
try
{
response = m_clientProxy.GetEmployeeInfo(req);
}
catch (WsFaultException ex)
{
Debug.Print("DPWS Fault: " + ex.Message);
}
finally
{
m_clientProxy.Dispose();
}
}
}
}
namespace OvasysImageClient
{
public class Program : Microsoft.SPOT.Application
{
//Windows
private Window mainWindow;
private Image imageView;
public static void Main()
{
Program myApplication = new Program();
Window mainWindow = myApplication.CreateWindow();
// Create the object that configures the GPIO pins to buttons.
GPIOButtonInputProvider inputProvider = new GPIOButtonInputProvider(null);
// Start the application
myApplication.Run(mainWindow);
}
public void GetImage(ClientApp test)
{
NetworkInterface networkInterface;
while (true)
{
networkInterface = NetworkInterface.GetAllNetworkInterfaces()[0];
if (networkInterface.IPAddress != "0.0.0.0") break;
Thread.Sleep(1000);
}
test.Run();
Debug.Print(test.response.GetEmployeeInfoResult.ID.ToString());
Debug.Print(test.response.GetEmployeeInfoResult.Name.ToString());
//Deserialize(test.response.GetEmployeeInfoResult.photo);
}
public Window CreateWindow()
{
// Create a window object and set its size to the
// size of the display.
mainWindow = new Window();
mainWindow.Height = SystemMetrics.ScreenHeight;
mainWindow.Width = SystemMetrics.ScreenWidth;
mainWindow.Background = new SolidColorBrush(Color.Black);
imageView = new Image();
imageView.HorizontalAlignment = Microsoft.SPOT.Presentation.HorizontalAlignment.Center;
imageView.VerticalAlignment = Microsoft.SPOT.Presentation.VerticalAlignment.Center;
mainWindow.Child = imageView;
// Connect the button handler to all of the buttons.
mainWindow.AddHandler(Buttons.ButtonUpEvent, new RoutedEventHandler(OnButtonUp), false);
// Set the window visibility to visible.
mainWindow.Visibility = Visibility.Visible;
// Attach the button focus to the window.
Buttons.Focus(mainWindow);
return mainWindow;
}
private void OnButtonUp(object sender, RoutedEventArgs evt)
{
ClientApp test = new ClientApp();
GetImage(test);
}
public void Deserialize(byte[] byteArray)
{
if (byteArray.Length > 0)
{
mainWindow.Background = new SolidColorBrush(Colors.Black);
imageView.Bitmap = new Bitmap(byteArray, Bitmap.BitmapImageType.Jpeg);
imageView.Invalidate();
}
}
}
}
The next few classes are generated using MFSvcUtil, and are on client side
namespace InfoService
{
[DataContract(Namespace="http://192.168.0.101/InfoService")]
public class GetEmployeeInfo
{
}
public class GetEmployeeInfoDataContractSerializer : DataContractSerializer
{
public GetEmployeeInfoDataContractSerializer(string rootName, string rootNameSpace) :
base(rootName, rootNameSpace)
{
}
public GetEmployeeInfoDataContractSerializer(string rootName, string rootNameSpace, string localNameSpace) :
base(rootName, rootNameSpace, localNameSpace)
{
}
public override object ReadObject(XmlReader reader)
{
GetEmployeeInfo GetEmployeeInfoField = null;
if (IsParentStartElement(reader, false, true))
{
GetEmployeeInfoField = new GetEmployeeInfo();
reader.Read();
reader.ReadEndElement();
}
return GetEmployeeInfoField;
}
public override void WriteObject(XmlWriter writer, object graph)
{
GetEmployeeInfo GetEmployeeInfoField = ((GetEmployeeInfo)(graph));
if (WriteParentElement(writer, true, true, graph))
{
writer.WriteEndElement();
}
return;
}
}
[DataContract(Namespace="http://192.168.0.101/InfoService")]
public class EmployeeInfo
{
[DataMember(Order=0, IsRequired=false)]
public int ID;
[DataMember(Order=1, IsNillable=true, IsRequired=false)]
public string Name;
}
public class EmployeeInfoDataContractSerializer : DataContractSerializer
{
public EmployeeInfoDataContractSerializer(string rootName, string rootNameSpace) :
base(rootName, rootNameSpace)
{
}
public EmployeeInfoDataContractSerializer(string rootName, string rootNameSpace, string localNameSpace) :
base(rootName, rootNameSpace, localNameSpace)
{
}
public override object ReadObject(XmlReader reader)
{
EmployeeInfo EmployeeInfoField = null;
if (IsParentStartElement(reader, false, true))
{
EmployeeInfoField = new EmployeeInfo();
reader.Read();
if (IsChildStartElement(reader, "ID", false, false))
{
reader.Read();
EmployeeInfoField.ID = XmlConvert.ToInt32(reader.ReadString());
reader.ReadEndElement();
}
if (IsChildStartElement(reader, "Name", true, false))
{
reader.Read();
EmployeeInfoField.Name = reader.ReadString();
reader.ReadEndElement();
}
reader.ReadEndElement();
}
return EmployeeInfoField;
}
public override void WriteObject(XmlWriter writer, object graph)
{
EmployeeInfo EmployeeInfoField = ((EmployeeInfo)(graph));
if (WriteParentElement(writer, true, true, graph))
{
if (WriteChildElement(writer, "ID", false, false, EmployeeInfoField.ID))
{
writer.WriteString(XmlConvert.ToString(EmployeeInfoField.ID));
writer.WriteEndElement();
}
if (WriteChildElement(writer, "Name", true, false, EmployeeInfoField.Name))
{
writer.WriteString(EmployeeInfoField.Name);
writer.WriteEndElement();
}
writer.WriteEndElement();
}
return;
}
}
[DataContract(Namespace="http://192.168.0.101/InfoService")]
public class GetEmployeeInfoResponse
{
[DataMember(Order=0, IsNillable=true, IsRequired=false)]
public EmployeeInfo GetEmployeeInfoResult;
}
public class GetEmployeeInfoResponseDataContractSerializer : DataContractSerializer
{
public GetEmployeeInfoResponseDataContractSerializer(string rootName, string rootNameSpace) :
base(rootName, rootNameSpace)
{
}
public GetEmployeeInfoResponseDataContractSerializer(string rootName, string rootNameSpace, string localNameSpace) :
base(rootName, rootNameSpace, localNameSpace)
{
}
public override object ReadObject(XmlReader reader)
{
GetEmployeeInfoResponse GetEmployeeInfoResponseField = null;
if (IsParentStartElement(reader, false, true))
{
GetEmployeeInfoResponseField = new GetEmployeeInfoResponse();
reader.Read();
EmployeeInfoDataContractSerializer GetEmployeeInfoResultDCS = new EmployeeInfoDataContractSerializer("GetEmployeeInfoResult", "http://192.168.0.101/InfoService", "http://192.168.0.101/InfoService");
GetEmployeeInfoResultDCS.BodyParts = this.BodyParts;
GetEmployeeInfoResponseField.GetEmployeeInfoResult = ((EmployeeInfo)(GetEmployeeInfoResultDCS.ReadObject(reader)));
reader.ReadEndElement();
}
return GetEmployeeInfoResponseField;
}
public override void WriteObject(XmlWriter writer, object graph)
{
GetEmployeeInfoResponse GetEmployeeInfoResponseField = ((GetEmployeeInfoResponse)(graph));
if (WriteParentElement(writer, true, true, graph))
{
EmployeeInfoDataContractSerializer GetEmployeeInfoResultDCS = new EmployeeInfoDataContractSerializer("GetEmployeeInfoResult", "http://192.168.0.101/InfoService", "http://192.168.0.101/InfoService");
GetEmployeeInfoResultDCS.BodyParts = this.BodyParts;
GetEmployeeInfoResultDCS.WriteObject(writer, GetEmployeeInfoResponseField.GetEmployeeInfoResult);
writer.WriteEndElement();
}
return;
}
}
[ServiceContract(Namespace="http://192.168.0.101/InfoService")]
public interface IIInfoService
{
[OperationContract(Action="http://192.168.0.101/InfoService/IInfoService/GetEmployeeInfo")]
GetEmployeeInfoResponse GetEmployeeInfo(GetEmployeeInfo req);
}
}
namespace InfoService
{
public class IInfoServiceClientProxy : DpwsClient
{
private IRequestChannel m_requestChannel = null;
public IInfoServiceClientProxy(Binding binding, ProtocolVersion version) :
base(binding, version)
{
// Set client endpoint address
m_requestChannel = m_localBinding.CreateClientChannel(new ClientBindingContext(m_version));
}
public virtual GetEmployeeInfoResponse GetEmployeeInfo(GetEmployeeInfo req)
{
// Create request header
String action;
action = "http://192.168.0.101/InfoService/IInfoService/GetEmployeeInfo";
WsWsaHeader header;
header = new WsWsaHeader(action, null, EndpointAddress, m_version.AnonymousUri, null, null);
WsMessage request = new WsMessage(header, req, WsPrefix.None);
// Create request serializer
GetEmployeeInfoDataContractSerializer reqDcs;
reqDcs = new GetEmployeeInfoDataContractSerializer("GetEmployeeInfo", "http://192.168.0.101/InfoService");
request.Serializer = reqDcs;
request.Method = "GetEmployeeInfo";
// Send service request
m_requestChannel.Open();
WsMessage response = m_requestChannel.Request(request);
m_requestChannel.Close();
// Process response
GetEmployeeInfoResponseDataContractSerializer respDcs;
respDcs = new GetEmployeeInfoResponseDataContractSerializer("GetEmployeeInfoResponse", "http://192.168.0.101/InfoService");
GetEmployeeInfoResponse resp;
resp = ((GetEmployeeInfoResponse)(respDcs.ReadObject(response.Reader)));
return resp;
}
}
}
namespace InfoService
{
public class IInfoService : DpwsHostedService
{
private IIInfoService m_service;
public IInfoService(IIInfoService service, ProtocolVersion version) :
base(version)
{
// Set the service implementation properties
m_service = service;
// Set base service properties
ServiceNamespace = new WsXmlNamespace("iin", "http://192.168.0.101/InfoService");
ServiceID = "urn:uuid:1f05ebe8-0ab3-46ce-ab1b-dd9bf95fd1a3";
ServiceTypeName = "IInfoService";
// Add service types here
ServiceOperations.Add(new WsServiceOperation("http://192.168.0.101/InfoService/IInfoService", "GetEmployeeInfo"));
// Add event sources here
}
public IInfoService(IIInfoService service) :
this(service, new ProtocolVersion10())
{
}
public virtual WsMessage GetEmployeeInfo(WsMessage request)
{
// Build request object
GetEmployeeInfoDataContractSerializer reqDcs;
reqDcs = new GetEmployeeInfoDataContractSerializer("GetEmployeeInfo", "http://192.168.0.101/InfoService");
GetEmployeeInfo req;
req = ((GetEmployeeInfo)(reqDcs.ReadObject(request.Reader)));
// Create response object
// Call service operation to process request and return response.
GetEmployeeInfoResponse resp;
resp = m_service.GetEmployeeInfo(req);
// Create response header
WsWsaHeader respHeader = new WsWsaHeader("http://192.168.0.101/InfoService/IInfoService/GetEmployeeInfoResponse", request.Header.MessageID, m_version.AnonymousUri, null, null, null);
WsMessage response = new WsMessage(respHeader, resp, WsPrefix.Wsdp);
// Create response serializer
GetEmployeeInfoResponseDataContractSerializer respDcs;
respDcs = new GetEmployeeInfoResponseDataContractSerializer("GetEmployeeInfoResponse", "http://192.168.0.101/InfoService");
response.Serializer = respDcs;
return response;
}
}
}
Finally this is the wsdl
<?xml version="1.0" encoding="UTF-8"?>
-<wsdl:definitions xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:i0="http://tempuri.org/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:tns="http://192.168.0.101/InfoService" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://192.168.0.101/InfoService" name="InfoService"><wsdl:import location="http://192.168.0.101:8090/InfoService.svc?wsdl=wsdl0" namespace="http://tempuri.org/"/>-<wsdl:types>-<xsd:schema targetNamespace="http://192.168.0.101/InfoService/Imports"><xsd:import namespace="http://192.168.0.101/InfoService" schemaLocation="http://192.168.0.101:8090/InfoService.svc?xsd=xsd0"/><xsd:import namespace="http://schemas.microsoft.com/2003/10/Serialization/" schemaLocation="http://192.168.0.101:8090/InfoService.svc?xsd=xsd1"/></xsd:schema></wsdl:types>-<wsdl:message name="IInfoService_GetEmployeeInfo_InputMessage"><wsdl:part name="parameters" element="tns:GetEmployeeInfo"/></wsdl:message>-<wsdl:message name="IInfoService_GetEmployeeInfo_OutputMessage"><wsdl:part name="parameters" element="tns:GetEmployeeInfoResponse"/></wsdl:message>-<wsdl:portType name="IInfoService">-<wsdl:operation name="GetEmployeeInfo"><wsdl:input message="tns:IInfoService_GetEmployeeInfo_InputMessage" wsaw:Action="http://192.168.0.101/InfoService/IInfoService/GetEmployeeInfo"/><wsdl:output message="tns:IInfoService_GetEmployeeInfo_OutputMessage" wsaw:Action="http://192.168.0.101/InfoService/IInfoService/GetEmployeeInfoResponse"/></wsdl:operation></wsdl:portType>-<wsdl:service name="InfoService">-<wsdl:port name="wsEndpoint" binding="i0:wsEndpoint"><soap12:address location="http://192.168.0.101:8090/InfoService.svc"/>-<wsa10:EndpointReference><wsa10:Address>http://192.168.0.101:8090/InfoService.svc</wsa10:Address></wsa10:EndpointReference></wsdl:port></wsdl:service></wsdl:definitions>
hope you can help me,
Thanks