Using single windows service to host 4 different WCF projects. How? - c#

Have above 5 WCF projects. In which 4 has to be hosted as windows service and 1 in IIS. All in the same machine.
For each of the 4 WCF projects, I require 4 Windows service projects to host separately. To minimize the number of projects to be maintained, I am thinking of one single windows service to install all the 4 WCF projects for easy maintainability. Anyway apart from OnStart and OnStop I call the wcf and no other logics are there.
The challenges I see are, each Windows service requires the same app config file as used in WCF projects. If I would do this dynamically by getting the service name from app settings, How will I load the app.config file of different wcf projects to host as windows service during run time.
Is this feasible ? If so how can i achieve this ?

Yes, it's possible, you just need to configure each one endpoint in your config file. How to do that:
Windows Service WCF hoster is ServiceHost class, so you need to create 4 hosts for every contract.
var serviceHost = new ServiceHost(typeof(CommunicationManagement));
serviceHost.Open()
Now you can configure every your service endpoint in services section:
<system.serviceModel>
<services>
<service name="Communication.Service.CommunicationManagement">
<endpoint
binding="netTcpBinding"
bindingConfiguration="myBinding"
contract="Communication.Service.ICommunicationManagement"
name="CommunicationManagement">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="MEX" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/Communication/Service/CommunicationManagement" />
</baseAddresses>
</host>
</service>
<service bname="Communication.Service.Managers.PhoneAdatpersManager">
<endpoint
binding="netTcpBinding"
bindingConfiguration="myBinding"
contract="Communication.IPhoneAdministration"
name="PhoneAdministration">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="MEX" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/Communication/Service/PhoneAdministration" />
</baseAddresses>
</host>
</service>
</services>
<system.serviceModel>
Above you can see PhoneAdatpersManager and CommunicationManagement services, they hosting in a single Windows Service and works together on 8000 port (but you can use different ports).

Related

Multiple services on same baseAddress using ninject.extension.wcf selfhost

Using the example from github, setting up a simple wcf service hosted in a windows service works fine. However, when I add another servicecontract implementation, I'm not able to host this under the same baseAddress, i.e from app.config:
<service name="ContractService" behaviorConfiguration="WebServicesBehavior">
<host>
<baseAddresses >
<add baseAddress="baseUri"/>
</baseAddresses>
</host>
<endpoint address="addr1" binding="wsHttpBinding" contract="IContract1"/>
<endpoint address="addr2" binding="wsHttpBinding" contract="IContract2"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
I'm able to host 2 services, using this example, however I would like to achieve to have multiple service contracts hosted within the same baseAddress and possible multiple endpoints, but my search for solutions to this have left me stranded.
This setup works:
<services>
<service name="ContractService1" behaviorConfiguration="WebServicesBehavior">
<host>
<baseAddresses >
<add baseAddress="http://localhost:8082/Services/"/>
</baseAddresses>
</host>
<endpoint address="addr1" binding="wsHttpBinding" contract="IContract1"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
<service name="ContractService2" behaviorConfiguration="WebServicesBehavior">
<host>
<baseAddresses >
<add baseAddress="http://localhost:8083/Services/"/>
</baseAddresses>
</host>
<endpoint address="addr2" binding="wsHttpBinding" contract="IContract2"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
The wireup using ninject is the same as in the github example.
Is there a way to achieve multiple endpoint using selfhost wcf with ninject?
You can have multiple services on the same port by specifying different baseAddresses for the same port.
Append the address from the endpoint definition to the base address for each of the services like so:
<add baseAddress="http://localhost:8083/Services/addr1"/>
<endpoint address="" binding="wsHttpBinding" contract="IContract1"/>

Hosting C# "Service Library" in Azure

I have a C# WCF Service Library that's running properly locally. A client running locally was able to see the service and consume it.
However, when I tried hosting the service in a WebRole and deploying it on Azure, the client was never able to connect to it successfully the same way it did locally! The way I did it was as follows:
// The WCF Service library is in namespace: Root.Library
Uri url = new Uri("http://xxx.cloudapp.net:8099/DealerService"); // tried also without "DealerService" but still didn't work
ServiceHost host = new ServiceHost(typeof(Root.Library.DealerService), url));
host.AddServiceEndpoint(typeof(Root.Library.IDealerService), new WSHttpBinding(), "DealerService");
ServiceMetadataBehavior dealerMetaBehavior = new ServiceMetadataBehavior();
dealerMetaBehavior.HttpGetEnabled = true;
host.Description.Behaviors.Add(dealerMetaBehavior);
host.Open();
And inside Root.Library, the app.config:
<service name="Root.Library.DealerService">
<endpoint address="" binding="wsHttpBinding" name="dealer" contract="Root.Library.IDealerService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" name="mex" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://xxx.cloudapp.net:8099/" />
</baseAddresses>
</host>
</service>
I also added an Endpoint to the WebRole config (Type:Input, Protocol:http, PublicPort:8099)
Can someone tell me what the problem might be??? this is really confusing me!
Thanks a lot!
You should just be able to change the port based on the csdef file
<Endpoints>
<InputEndpoint name="Endpoint1" protocol="http" port="8888" />
</Endpoints>
Saqib Ullah has a post on geekswithblogs that shows more information: http://geekswithblogs.net/technetbytes/archive/2011/04/25/145035.aspx

How to publish WCF service in internet (hosted in wnd service)

I have a windows service on my laptop, which hosts a WCF service. I would like to use these service on my ASP.NET website, which is on external ASP.NET server.
Could you help me, how to do this?
Is it necessary a specific laptop configuration for that? What should I configure?
And binding, what type will adequate? .. Right now, I've got:
<service behaviorConfiguration="WcfServices.InfoBehavior" name="MyProgram.WcfServices.Info.Info">
<endpoint address="" binding="wsHttpBinding" contract="MyProgram.WcfServices.Info.IInfo">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/Info/" />
</baseAddresses>
</host>
</service>
UPDATE:
Right now, my client app is still on my laptop (it is not publish yet).. This is my client code:
<client>
<endpoint address="http://localhost:8732/Info/" binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IInfo" contract="ServiceInfo.IInfo"
name="WSHttpBinding_IInfo">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
I don't know, what binding use.. what port, what settings should be changed on my laptop?
Unless your laptop has its own fixed IP address exposed externally (most unlikely) I think you will find it hard to do this directly.
You might consider using Azure Service Bus to broker message exchanges: I believe this is one way to solve the problem of accessing a service hosted on a non-constant IP address or behind a firewall/NAT.
Or you could consider changing your design to turn things the other way around. That is, when it is connected and running, your laptop service connects to a service hosted on the ASP.NET box, over a duplex binding, with your current service contract as the callback contract.
If you have a WCF service running on your laptop hosted via ServiceHost you'll need to duplicate that configuration in your ASP.NET web.config file, as well as add a "service.svc" file which is referenced to the Interface of your service.
You should change localhost with real external facing IP address of your laptop and it could work if your router at home has no firewall. Change it in both client and server endpoint address.

Question on how to create WCF silverlight 3 endpoints

I wanted to consume a WCF service with a silverlight application and a asp.net mvc application, and I'm having difficulties to configure the service to support both requests.
these are my endpoints for the WCF config file.
<service behaviorConfiguration="behaviorAction" name="Uniarchitecture.ProdutoService.ServiceImplementations.ProdutoService">
<endpoint binding="wsHttpBinding" bindingConfiguration="bindingAction" contract="Uniarchitecture.ProdutoService.ServiceContracts.IProdutoService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="" binding="basicHttpBinding" contract="Uniarchitecture.ProdutoService.ServiceContracts.IProdutoService"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
I'm getting the following error:
A binding instance has already been associated to listen URI 'net.tcp://localhost:10377/ProdutoService'. If two endpoints want to share the same ListenUri, they must also share the same binding object instance. The two conflicting endpoints were either specified in AddServiceEndpoint() calls, in a config file, or a combination of AddServiceEndpoint() and config.
The problem is trying to use two endpoints with two bindings... You can use multiple endpoints on the same service here, but they need to use the same binding.
And since Silverlight only supports BasicHttpBinding you're kind of stuck with it.
<service behaviorConfiguration="behaviorAction" name="Uniarchitecture.ProdutoService.ServiceImplementations.ProdutoService">
<endpoint binding="**basic**HttpBinding" bindingConfiguration="bindingAction" contract="Uniarchitecture.ProdutoService.ServiceContracts.IProdutoService"/>
<endpoint address="" binding="basicHttpBinding" contract="Uniarchitecture.ProdutoService.ServiceContracts.IProdutoService"/>
</service>
In your config, the addresses of the two endpoints are the same. With HTTP bindings, you can have multiple endpoints for a service, but you need to specify different addresses for them. Change the address of the basicHttpBinding endpoint to fix this problem.

Service is not accessible using netTcpBinding?

This is the configuration file while hosting the service, here i hosted service using ip 127.0.0.1:36345, here service is not accessible using netTcpBinding
when i hosted the using the exact ip of server (192.168.1.47:36345) it works..
what could be possible reason and solution?
<services>
<service behaviorConfiguration="metadataSupport" name="WCFSvc.WCFService">
<endpoint binding="wsDualHttpBinding" bindingConfiguration="wsDualHttp"
contract="WCFSvc.IWCFService" />
<endpoint binding="netTcpBinding" bindingConfiguration="netTcp"
contract="WCFSvc.IWCFService" />
<endpoint binding="netNamedPipeBinding" contract="WCFSvc.IWCFService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://127.0.0.1:26345/WCFSvc" />
<add baseAddress="net.tcp://127.0.0.1:36345/WCFSvc" />
<add baseAddress="net.pipe://localhost/DataService/Service.svc" />
</baseAddresses>
</host>
</service>
</services>
Could be several things, here are a few that you could try:
Is there any local firewall on the machine blocking the request?
Is there a confilct with the net.pipe configuration?
Is there something in the binding configuration that could be causing the problem?
What error are you getting? Is there any error being written to the eventlog?

Categories

Resources