Main Site Documentation

How to start a hosted WCF server


#1

Hi,
I just try MF WCF sample on my card, but it always pops out below error message. I even comment out all code except the Device. Initialize() and Device.Start(), no any change to code, it still can’t work.
Pls help, thx.

BTW, I am running with .Net Micro Framework 4.1


    #### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (1) ####
    #### Message: 
    #### Microsoft.SPOT.Net.SocketNative::sendto [IP: 0000] ####
    #### System.Net.Sockets.Socket::SendTo [IP: 0022] ####
    #### System.Net.Sockets.Socket::SendTo [IP: 000a] ####
    #### Dpws.Device.Discovery.DpwsDiscoGreeting::SendGreetingMessage [IP: 0087] ####
    #### Dpws.Device.Device::Start [IP: 0020] ####
    #### Dpws.Device.Program::Start [IP: 0045] ####
    #### Dpws.Device.Program::Main [IP: 0033] ####
    #### SocketException ErrorCode = 10049
    #### SocketException ErrorCode = 10049
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.SPOT.Net.dll
    #### SocketException ErrorCode = 10049
    #### SocketException ErrorCode = 10049


#2

WSAEADDRNOTAVAIL (10049) Cannot assign requested address.

You have an address problem.


#3

I run another sample with socket, with same IP address, it can work.
I also read Device.IPv4Address = "192.168.1.101"
So what’s this mean can’t assign address, how can I solve it?

thx


#4

Could you post your code please? I’m not sure which example you are using…

P.S. Is this the same problem that you have?

http://www.netmf.com/forum/default.aspx?g=posts&m=8286&find=lastpost#post8286


#5

Yes, I saw that post, but it also had no solution, so I posted here for help.

And below is the code from .Net Micro Framework sample, I just comment out hosted service for confirming the problem is wcf server itself .



        public void Start()
        {
            // Initialize the binding
            //Guid g = Guid.NewGuid();
            string guid = "urn:uuid:18571766-87df-06e2-bb68-5136c48f483f";

            // ProtocolVersion10 can be used only if the corresponding HelloWorldClient_WCF application
            // uses a custom binding with Soap12WSAddressingAugust2004 text message encoding.
            ProtocolVersion version = new ProtocolVersion11();
            WS2007HttpBinding binding = new WS2007HttpBinding(new HttpTransportBindingConfig(guid, 8084));
            // maybe due to this returns empty:WsNetworkServices.GetLocalIPV4Address()
            string uri = binding.Transport.EndpointAddress.AbsoluteUri;
            Device.Initialize(binding, version);
            //Device.Initialize(new WS2007HttpBinding(new HttpTransportBindingConfig(new System.Uri("http://192.168.1.101:8084", UriKind.Absolute))), version);
            
            // Set device information
            //Device.ThisModel.Manufacturer = "GHI Electronics";
            //Device.ThisModel.ManufacturerUrl = "http://www.GHI Electronics.com/";
            //Device.ThisModel.ModelName = "Chipworks MINI9261I";
            //Device.ThisModel.ModelNumber = "1.4";
            //Device.ThisModel.ModelUrl = "http://www.GHI Electronics.com/";
            //Device.ThisModel.PresentationUrl = "http://www.GHI Electronics.com/";

            //Device.ThisDevice.FriendlyName = "HelloWorldService";
            //Device.ThisDevice.FirmwareVersion = "4.1.7.0";
            //Device.ThisDevice.SerialNumber = "32345678";
            string ip = Device.IPV4Address;

            // Add a Host service type
            //Device.Host = new HelloWCFService(version);

            // Add Dpws hosted service(s) to the device
            //Device.HostedServices.Add(new IServiceHelloWCF(new wcfTestImplement()));

            // Set this device property if you want to ignore this clients request
            Device.IgnoreLocalClientRequest = true;

            // Turn console messages on
            Console.Verbose = true;

            System.Ext.Console.Write("Start DPWS device service with endpoint address: '" + Device.EndpointAddress + "'");

            ServerBindingContext ctx = new ServerBindingContext(version);

            try
            {
                // Start the device
                Device.Start(ctx);

            }
            catch (Exception e)
            {
                System.Ext.Console.Write(e.ToString());
                throw;
            }
        }


#6

When I used your code like that I got the same exception. I added a few lines to initialize the network first… although this is hardcoded to use the first network device so if you have several (like a wifi extension) you’d have some more work to do.


using System;
using Microsoft.SPOT;

using Dpws.Device;
using Ws.Services;
using Ws.Services.Binding;

using System.Ext;


namespace TestWPF
{
    public class Program
    {
        public static void Main()
        {
            Microsoft.SPOT.Net.NetworkInformation.NetworkInterface NI = Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0];
            if (NI.IsDhcpEnabled == false)
            {
                Debug.Print("Enabling DHCP.");
                NI.EnableDhcp();
                Debug.Print("DCHP - IP Address = " + NI.IPAddress + " ... Net Mask = " + NI.SubnetMask + " ... Gateway = " + NI.GatewayAddress);
            }
            else
            {
                Debug.Print("Renewing DHCP lease.");
                NI.RenewDhcpLease();
                Debug.Print("DCHP - IP Address = " + NI.IPAddress + " ... Net Mask = " + NI.SubnetMask + " ... Gateway = " + NI.GatewayAddress);
            }
            // Initialize the binding
            //Guid g = Guid.NewGuid();
            string guid = "urn:uuid:18571766-87df-06e2-bb68-5136c48f483f";

            // ProtocolVersion10 can be used only if the corresponding HelloWorldClient_WCF application
            // uses a custom binding with Soap12WSAddressingAugust2004 text message encoding.
            ProtocolVersion version = new ProtocolVersion11();
            WS2007HttpBinding binding = new WS2007HttpBinding(new HttpTransportBindingConfig(guid, 8084));
            // maybe due to this returns empty:WsNetworkServices.GetLocalIPV4Address()
            string uri = binding.Transport.EndpointAddress.AbsoluteUri;
            Device.Initialize(binding, version);
            //Device.Initialize(new WS2007HttpBinding(new HttpTransportBindingConfig(new System.Uri("http://192.168.1.101:8084", UriKind.Absolute))), version);

            // Set device information
            //Device.ThisModel.Manufacturer = "GHI Electronics";
            //Device.ThisModel.ManufacturerUrl = "http://www.GHI Electronics.com/";
            //Device.ThisModel.ModelName = "Chipworks MINI9261I";
            //Device.ThisModel.ModelNumber = "1.4";
            //Device.ThisModel.ModelUrl = "http://www.GHI Electronics.com/";
            //Device.ThisModel.PresentationUrl = "http://www.GHI Electronics.com/";

            //Device.ThisDevice.FriendlyName = "HelloWorldService";
            //Device.ThisDevice.FirmwareVersion = "4.1.7.0";
            //Device.ThisDevice.SerialNumber = "32345678";
            string ip = Device.IPV4Address;
            Console.Write(ip);

            // Add a Host service type
            //Device.Host = new HelloWCFService(version);

            // Add Dpws hosted service(s) to the device
            //Device.HostedServices.Add(new IServiceHelloWCF(new wcfTestImplement()));

            // Set this device property if you want to ignore this clients request
            Device.IgnoreLocalClientRequest = true;

            // Turn console messages on
            Console.Verbose = true;

            System.Ext.Console.Write("Start DPWS device service with endpoint address: '" + Device.EndpointAddress + "'");

            ServerBindingContext ctx = new ServerBindingContext(version);

            try
            {
                // Start the device
                Device.Start(ctx);

            }
            catch (Exception e)
            {
                System.Ext.Console.Write(e.ToString());
                throw;
            }
        }
    }
}


Let me know if that helps…


#7

Xykon:

Thx very much!
I knew the issue should be trivial, but it just confusing so much. I now run it successfully as I add some network configuration into it.

I think it should be wrong setting with WFDeploy to set network.

after I added below lines of code, it works.


            (NetworkInterface.GetAllNetworkInterfaces())[0].PhysicalAddress = mac;
            (NetworkInterface.GetAllNetworkInterfaces())[0].EnableStaticIP("192.168.1.101","255.255.255.0", "192.168.0.1");



#8

So now, the wcf service can run on device, but another issue comes :’(

When i run the sample WCF client on PC side, it shows error:

No route can be determined to reach the destination role defined by the WS=Addressing To

When I run code
ServiceHelloWCFClient client = new ServiceHelloWCFClient(“IServiceHelloWCF_IServiceHelloWCF”);

        string resp = client.HelloWCF("World");

That error occurs, is it still a hardware error? But if I run client with a MF emulator, it can work.

Below is my configuration file setting :


  <system.serviceModel>
    <bindings>
      <customBinding>
        <binding name="Soap12AddressingBinding" >
          <textMessageEncoding messageVersion="Soap12WSAddressingAugust2004" />
          <httpTransport manualAddressing="false" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" allowCookies="false" authenticationScheme="Anonymous" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" keepAliveEnabled="false" maxBufferSize="65536" proxyAuthenticationScheme="Anonymous" realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false" useDefaultWebProxy="true"/>
        </binding>
      </customBinding>
      <wsHttpBinding>
        <binding name="NewBinding0">
          <security mode="None">
            <transport clientCredentialType="None" />
            <message clientCredentialType="None" negotiateServiceCredential="false"
              establishSecurityContext="false" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <client>
      <endpoint binding="wsHttpBinding" bindingConfiguration="NewBinding0"
        contract="IServiceHelloWCF" name="DefaultBinding_IServiceHelloWCF_IServiceHelloWCF" />

      <endpoint address="http://192.168.1.101:8084/18571766-87df-06e2-bb68-5136c48f483f" binding="customBinding"
          bindingConfiguration="Soap12AddressingBinding" contract="IServiceHelloWCF"
          name="IServiceHelloWCF_IServiceHelloWCF" />
    </client>
  </system.serviceModel>