is it possible to implement multiple wcf services( multiple http: scheme) - c#

Is it possible to implement multiple services with the same scheme HTTP?
Like this:
<system.serviceModel>
<services>
<service name="MyServiceBecouseError.MyNameService" behaviorConfiguration="mexBehaviors">
<endpoint address="xxx" binding="basicHttpBinding" contract="MyServiceBecouseError.IMyNameService"></endpoint>
<endpoint address="yyy" binding="basicHttpBinding" contract="MyServiceBecouseError.IMyNameServiceSecond"></endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/eff-off"/>
<add baseAddress="http://localhost:5555/eff-off"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>

Yes, it's possible.
As a result you'll have your two services listening at:
http://localhost:8080/eff-off/xxx
http://localhost:8080/eff-off/yyy
http://localhost:5555/eff-off/xxx
http://localhost:5555/eff-off/yyy
And of course metadata exchange endpoints will be there as well.

Related

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

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).

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"/>

Can I use netTcpBinding for WCF services hosted outside of IIS?

My WCF service is being hosted as a Windows managed service, so I'm unsure of whether or not I can still use the netTcpBinding. I've tried following a couple of guides at MSDN, but for some reason my service always fails to start whenever I do the switch from basicHttpBinding. Perhaps there are additional steps that services outside of the IIS are required to undergo?
Yes you can host WCF service with netTcpBinding outside of IIS, in Windows service or even Console Application if you want to.
Here is config file sample:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="ServiceBehavior"
name="XX.XX.Service">
<endpoint address=""
binding="netTcpBinding"
bindingConfiguration="BindingConfiguration"
contract="XX.XX..IService" />
<endpoint address="mex"
binding="mexTcpBinding"
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8731/XXService" />
</baseAddresses>
</host>
</service>
</services>
<bindings>
<netTcpBinding>
<binding
name="BindingConfiguration">
<security mode="None" />
</binding>
</netTcpBinding>
</bindings>
</system.serviceModel>
[Edit]
Problems with your config file:
base address is http instead of net.tcp
metadata endpoint is mexHttpBinding instead of metTcpBinding
security - by default windows authorization will be used, if test communication between to boxes, you might have permission problem. I suggest to start with security mode None and then adjust security when everything else works.
you don't need to specify httpGetEnabled for service behavior
if the port that you are going to use is already in use, you will not be able to start service
You absolutely can, and I'd go so far as to say you should.
Here's your problem:
<services>
<service name="Server.FileService" ...
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/Test/file"/>
</baseAddresses>
</host>
<endpoint address="" binding="netTcpBinding" contract="Server.IFile" />
<endpoint address="mex" binding="mexHttpBinding" ...
The net.tcp address must have a net.tcp:// prefix, not a http:// prefix.
I don't normally use baseAddress so can't give advice on that. I'd remove baseAddress and instead use
<endpoint address="net.tcp://localhost:8001/Test/file" ..
(note that I would also choose another port over 8000)

Discover WCF hosted by Windows Service

In the solution, 4 projects : Business, the WCFService and the WCFServiceHost (a windows service) and a client
When I work in the solution, no problem, I can discover and create the proxy in the client.
When I install the host service, I start it but impossible to discover and create the proxy from visual studio with this : net.tcp://localhost:9100/MyApplicationWcf
Any idea ?
Thanks,
update #1
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="MyApplicationWcf.MyClassWcf">
<endpoint address="net.tcp://localhost:9100/MyClassWcf"
binding="netTcpBinding"
bindingConfiguration=""
name="MyClassWcf_Tcp"
contract="MyApplicationWcf.MyClassWcf" />
<endpoint name="mexHttpBinding"
contract="IMetadataExchange"
binding="mexHttpBinding"
address="mex" />
</service>
</services>
</system.serviceModel>
</configuration>
If you want to discover the service over net.tcp, you will need to define a MEX endpoint (metadata exchange) that uses the mexTcpBinding.
<behaviors>
<serviceBehaviors>
<behavior name="MexBehavior" >
<serviceMetadata/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<netTcpBinding>
<binding name="mexBinding" portSharingEnabled="true">
<security mode="None"></security>
</binding>
</netTcpBinding>
</bindings>
<services>
<service name="YourServiceImpl"
behaviorConfiguration="MexBehavior">
<host>
<baseAddresses>
<add baseAddress ="net.tcp://localhost:9100/MyApplicationWcf/"/>
</baseAddresses>
</host>
<endpoint address=""
binding="netTcpBinding"
contract="IYourServiceContract" />
<endpoint address="mex"
binding="mexTcpBinding" bindingConfiguration="mexBinding"
contract="IMetadataExchange" />
</service>
</services>
Do you have that??
Check out: How to: Publish Metadata for a Service Using a Configuration File for more information.

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