开发者

How to do self hosting a wcf service and access it outside in different system

My requirement is I want to access a wcf service ou开发者_JAVA百科tside the server where I have developed that. I need to do selfhosting and we don't have IIS there, so i need to do it tcp hosting. Windows service host creating some problem. I want to access that from a different machine. please share any link or demo application will be great.


I have created an helper method which hosts my WCF Service taking the URI as parameter, here is the method:

 public ServiceHost CreateService(Uri baseAddress)
        {
            // create the net.tcp binding for the service endpoint
            NetTcpBinding ntcBinding = new NetTcpBinding();
            ntcBinding.Security.Mode = SecurityMode.None;
            System.ServiceModel.Channels.Binding tcpBinding = ntcBinding;

            // create the service host and add the endpoint
            ServiceHost host = new ServiceHost(typeof(RMS.Gateway.Services.RiskLinkService), baseAddress);

            host.Opening += new EventHandler(host_Opening);
            host.Opened += new EventHandler(host_Opened);
            host.Closing += new EventHandler(host_Closing);
            host.Closed += new EventHandler(host_Closed);
            host.Faulted += new EventHandler(host_Faulted);
            host.UnknownMessageReceived += new EventHandler<UnknownMessageReceivedEventArgs>(host_UnknownMessageReceived);

            // Check to see if the service host already has a ServiceMetadataBehavior
            ServiceMetadataBehavior smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>();

            // If not, add one
            if (smb == null)
                smb = new ServiceMetadataBehavior();

            //smb.HttpGetEnabled = true;

            smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;

            host.Description.Behaviors.Add(smb);

            // Add MEX endpoint
            host.AddServiceEndpoint(
              ServiceMetadataBehavior.MexContractName,
              MetadataExchangeBindings.CreateMexTcpBinding(),
              "mex"
            );

            host.AddServiceEndpoint(typeof(RAE.Entities.ServiceInterfaces.IRiskLinkService), ntcBinding, string.Empty);

            host.Open();

            return host;
        }

then from a test Console application I use this method to start the hosting, it could be done in a similar way inside a windows service, in production we will but for now and for simple debugging we host in the console app :)

Uri baseAddress = new Uri(ConfigurationManager.AppSettings["serviceURL"]);

        // Create the ServiceHost.
        using (ServiceHost host = serviceFactory.CreateService(baseAddress))
        {
            System.Console.WriteLine("The service is ready at {0}", baseAddress);
            System.Console.WriteLine("Press <Enter> to stop the service.");
            System.Console.ReadLine();

            // Close the ServiceHost.
            host.Close();
        }

as you can see NetTcpBinding is created at runtime from code, it works and we can access and use the WCF end points from other machines in the network.


Hope this step by step guide of microsoft can help you in hosting the wcf service and consuming the service.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜