Self Hosting WCF service using app.Config files - c#

I have self hosting WCF service that contains it's own app.Config to expose endpoints required for the service contracts. If the service is started in the programs.cs main method it all works just fine and the metadata is exposed via the browser. However, I created a HostService class based on the ServiceBase class which in the same host library and is instantiated within the program.cs file. The HostService class starts the service and has a timer method to ping other client web services for information.
My question is, when I created the HostService : ServiceBase class and instantiate it from the main(), I have to put a duplicate app.Config file in the Service Library in order for the endpoints to properly exposed and return the metadata/wsdl. I don't want to maintain 2 duplicate app.config files if possible. Currently the host library and service library both require one. Is there a way to only have just one w/ the host that could be used for both? Sorry for the dumb question, but I'm new to WCF =)
Program.cs
static void Main(string[] args){
var service = new HostService();
service.StartHostService(args);
}
HostService.cs
public partial class HostService : ServiceBase
{
internal void StartHostService(string[] args)
{
this.OnStart(args);
Console.ReadLine();
this.OnStop();
}
....
}

Short answer is no. There must be two configs, one for the client that consumes the WCF and one for the server that exposes that communication methods with the WCF.
In order for your client to work, your config must be set with Client Configuration
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<client>
<endpoint
name="endpoint1"
address="http://localhost/ServiceModelSamples/service.svc"
binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IHello"
behaviorConfiguration="IHello_Behavior"
contract="IHello" >
<metadata>
<wsdlImporters>
<extension
type="Microsoft.ServiceModel.Samples.WsdlDocumentationImporter, WsdlDocumentation"/>
</wsdlImporters>
</metadata>
<identity>
<servicePrincipalName value="host/localhost" />
</identity>
</endpoint>
// Add another endpoint by adding another <endpoint> element.
<endpoint
name="endpoint2">
//Configure another endpoint here.
</endpoint>
</client>
//The bindings section references by the bindingConfiguration endpoint attribute.
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IHello"
bypassProxyOnLocal="false"
hostNameComparisonMode="StrongWildcard">
<readerQuotas maxDepth="32"/>
<reliableSession ordered="true"
enabled="false" />
<security mode="Message">
//Security settings go here.
</security>
</binding>
<binding name="Another Binding"
//Configure this binding here.
</binding>
</wsHttpBinding>
</bindings>
//The behavior section references by the behaviorConfiguration endpoint attribute.
<behaviors>
<endpointBehaviors>
<behavior name=" IHello_Behavior ">
<clientVia />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
notice the <client> tag specifying how the client must call the WCF.
and with Server Config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="myBindingConfiguration1" closeTimeout="00:01:00" />
<binding name="myBindingConfiguration2" closeTimeout="00:02:00" />
<binding closeTimeout="00:03:00" /> <!—- Default binding for basicHttpBinding -->
</basicHttpBinding>
</bindings>
<services>
<service name="MyNamespace.myServiceType">
<endpoint
address="myAddress" binding="basicHttpBinding"
bindingConfiguration="myBindingConfiguration1"
contract="MyContract" />
<endpoint
address="myAddress2" binding="basicHttpBinding"
bindingConfiguration="myBindingConfiguration2"
contract="MyContract" />
<endpoint
address="myAddress3" binding="basicHttpBinding"
contract="MyContract" />
</service>
</services>
</system.serviceModel>
</configuration>
Notice there is no <client> tag here.

Related

C# .NetTcpBinding 2 services at same port 5000

I am hosting two services using NetTcpBinding on same port 5000 like below.
In my service app.config I have like below
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="netTcpBindingConfiguration"
closeTimeout="00:10:00"
openTimeout="00:10:00"
receiveTimeout="00:10:00"
sendTimeout="00:10:00"
maxBufferSize="2147483647"
maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647" />
</binding>
</netTcpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="netTcpServiceBehavior">
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<!--First Service [net.tcp://localhost:5000/MyService/FirstService] -->
<service behaviorConfiguration="netTcpServiceBehavior" name="FirstserviceLib">
<endpoint address="" binding="netTcpBinding" contract="IFirstService"
bindingConfiguration="netTcpBindingConfiguration" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:5000/MyService/FirstService" />
</baseAddresses>
</host>
</service>
<!--Second Service [net.tcp://localhost:5000/MyService/SecondService] -->
<service behaviorConfiguration="netTcpServiceBehavior" name="SecondserviceLib">
<endpoint address="" binding="netTcpBinding" contract="ISecondService"
bindingConfiguration="netTcpBindingConfiguration" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:5000/MyService/SecondService" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
I am using console application to self-host like below
static void Main(string[] args)
{
ServiceHost firstHost = new ServiceHost(typeof(IFirstService));
firstHost.Open();
ServiceHost secondHost = new ServiceHost(typeof(ISecondService));
secondHost.Open();
Console.WriteLine("Services Hosted");
}
When I run my console application I get Service Hosted message. I feel my services are running.
On client side, I have below in my app.config
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="netTcpBindingConfiguration"
closeTimeout="00:10:00"
openTimeout="00:10:00"
receiveTimeout="00:10:00"
sendTimeout="00:10:00"
maxBufferSize="2147483647"
maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647" />
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost:5000/MyService/SecondService"
binding="netTcpBinding"
contract="ISecondService"
name="NetTcpBinding_SecondService"
bindingConfiguration="netTcpBindingConfiguration" />
</client>
</system.serviceModel>
I am calling a method in SecondService like below
string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
Configuration config = ConfigurationManager.OpenExeConfiguration(path);
ConfigurationChannelFactory<ISecondService> chn =
new ConfigurationChannelFactory<ISecondService>(
"NetTcpBinding_SecondService",
config,
new EndpointAddress("net.tcp://localhost:5000/MyService/SecondService"));
ISecondService facade = chn.CreateChannel();
string fullName = facade.GetName();
I am getting exception like below
System.ServiceModel.EndpointNotFoundException: There was no endpoint listening at net.tcp://localhost:5000/MyService/SecondService that could accept the message. This is often caused by an incorrect address or SOAP action
Since I am working on Windows 10 I enabled and running below service
Note: If I host only 1 service everything is good. But when I add SecondService it is not working. Please let me know what mistake I am making.
I can test IFirstService but not ISecondService
UPDATE 1:
I opened command prompt and went to C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.7.2 Tools
I gave svcutil net.tcp://localhost:5000/MyService/SecondService
I do not get any error message like There was no endpoint listening at blah blah...
but when I give svcutil net.tcp://localhost:5000/MyService/FirstService
I do get error message like There was no endpoint listening at blah blah...
It seems my service itself is not hosted properly.
It works fine now.
In my consolehost app.config I made sure there are no spaces between <service> in <services> section.
I removed all references to FirstService in Main method in Program.cs of my console host and also in App.config.
I ran my consolehost exe and now ISecondService is hosted
I ran svcutil net.tcp://localhost:5000/MyService/SecondService and now I do not see endpoint not listening error message.
On client side, I was able to execute below line which threw exception in GetName()
ISecondService facade = chn.CreateChannel();
string fullName = facade.GetName();
I fixed my code and re-executed it and this time it was fine.
Now I re-added references to FirstService in Console Host app.config and in Main() of Program.cs
I ran consolehost exe to host both services and now both below did not show me no endpoint message
svcutil net.tcp://localhost:5000/MyService/FirstService
svcutil net.tcp://localhost:5000/MyService/SecondService
Now everything works fine.

Rename WCF Service Name & Binding Name

Assumption:
I'm working with a WCF application called DemoWCF, in which I have a simple wcf service called Calculator.
The purpose of the application is obviously a demo, the WCF service has to take two numbers as parameters and make the sum of them.
Service Class:
public class Calculator : ICalculator
{
public int Sum(int firstValue, int secondValue)
{
return firstValue + secondValue;
}
}
Service Contract :
[ServiceContract(Namespace="http://ROCKET/Calculator")]
public interface ICalculator
{
[OperationContract]
int Sum(int firstValue, int secondValue);
}
Web.config, service model configuration:
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="wsHttpsBinding">
<security mode="Transport">
<transport clientCredentialType="None"></transport>
</security>
</binding>
</wsHttpBinding>
</bindings>
<services>
<service name="DemoWCF.Services.ServiceClasses.Calculator">
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="wsHttpsBinding" contract="DemoWCF.Services.ServiceContracts.ICalculator" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<serviceMetadata httpsGetEnabled="true" httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
When I deploy the WCF application on my IIS and I try to consume the WCF service in SOAP UI or in an ASP.NET MVC application, everything works fine.
Goal:
When I consume the WCF service in SOAP UI, I note that from the wsdl of the web service is automatically generated the binding "WSHttpBinding_ICalculator".
When I consume the WCF service in an ASP.NET MVC application, I note that the from the wsdl of the web service is automatically generated the service model, precisely:
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_ICalculator">
<security mode="Transport">
<transport clientCredentialType="None" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="https://rocket/ServiceClasses/Calculator.svc" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ICalculator" contract="Calculator.ICalculator" name="WSHttpBinding_ICalculator" />
</client>
</system.serviceModel>
What I want to do, is to rename the Service Name and the Binding Name of the WCF Service.
It's possibile to omit this WCF behavior of generating binding's name with the format : "BindingType_IServiceInterface" ?
What I have to do, if I want to rename my service name and binding name in "Calculator" ?

How to call a service using multiple Endpoint URIs

To put into context, I have a client application that will attempt to call a webservice that will be deployed on multiple web servers. The URI list will be obtained from the Settings.settings file of the client and a foreach loop will cycle through the URIs until the available service responds.
Let's say I have a service with the following contract:
[ServiceContract]
public interface ICMMSManagerService
{
[OperationContract]
ServerInfo GetServerInfo(string systemNumber);
}
In the web.config of the service's project, I have defined CMMSManager service with the the endpoint name: BasicHttpBinding_IWorkloadMngrService
<system.serviceModel>
<services>
<service name="WorkloadMngr">
<endpoint binding="basicHttpBinding" contract="IMetadataExchange" />
</service>
<service name="CMMSManager">
<endpoint binding="basicHttpBinding" contract="IMetadataExchange" name="BasicHttpBinding_IWorkloadMngrService" />
</service>
</services>
<client>
<remove contract="IMetadataExchange" name="sb" />
</client>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding>
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
On the client side, I have the following code executed when the application starts:
private void QueryWebServiceUrls()
{
var webServiceUrls = Properties.Settings.Default.WebServiceUrls;
foreach (var webServiceUrl in webServiceUrls)
{
try
{
var client = new CMMSManagerServiceClient("BasicHttpBinding_IWorkloadManagerService");
client.Endpoint.Address = new EndpointAddress(new Uri(webServiceUrl),
client.Endpoint.Address.Identity, client.Endpoint.Address.Headers);
client.Open();
var result = client.GetServerInfo("test");
}
catch (EndpointNotFoundException e)
{
continue;
}
catch (InvalidOperationException e)
{
break;
}
}
}
But the application crashes with an InvalidOperationException when the CMMSManagerServiceClient class is instanciated.
Could not find endpoint element with name
'BasicHttpBinding_IWorkloadMngrService' and contract
'ComClientService.ICMMSManagerService' in the ServiceModel client
configuration section. This might be because no configuration file was
found for your application, or because no endpoint element matching
this name could be found in the client element.
I have the following configuration in the app.config:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_ICMMSManagerService">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost/WorkloadMngr/CMMSManagerService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ICMMSManagerService"
contract="ComClientService.ICMMSManagerService" name="BasicHttpBinding_ICMMSManagerService" />
</client>
</system.serviceModel>
I thought everything was valid by passing the BasicHttpBinding_ICMMSManagerService parameter to the CMMSManagerServiceClient class. I have no clue what am I missing at the moment... Any ideas?
The error is telling you exactly what's wrong: there is no endpoint with the name BasicHttpBinding_IWorkloadMngrService. The app.config says the endpoint is called BasicHttpBinding_ICMMSManagerService so your code should be:
var client = new CMMSManagerServiceClient("BasicHttpBinding_ICMMSManagerService");
Hope this helps.

Instantiating WCF Service Weird Error

I am trying to add a reference to a WCF service from my C# Desktop app.
I can add the service reference OK but as soon as I try to open the form within this desktop app I get this error:
NB. I have a UserControl that instantiates a reference to my WCF service and the control in in my GUI Form Class.
Could not find default endpoint element that references contract '' in
the ServiceModel client configuration section. This might be because
no configuration file was found for your application, or because no
endpoint element matching this contract could be found in the client
element.
This is in my app.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IMotionUpdater" messageEncoding="Mtom" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://a url/MotionUpdater.svc/MotionUpdater.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IMotionUpdater"
contract="wsCloudFeeder.IMotionUpdater" name="BasicHttpBinding_IMotionUpdater" />
</client>
</system.serviceModel>
</configuration>
And this is in my web.config file:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="basicHttpBindingEndPoint" maxReceivedMessageSize="10485760" messageEncoding="Mtom" closeTimeout="00:00:10" openTimeout="00:00:10" >
<readerQuotas maxArrayLength="32768"/>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="MotionUpdater" behaviorConfiguration="ThrottledBehavior">
<endpoint address="MotionUpdater.svc" binding="basicHttpBinding" bindingConfiguration="basicHttpBindingEndPoint" contract="IMotionUpdater"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ThrottledBehavior">
<serviceTimeouts transactionTimeout="1"/>
<serviceThrottling maxConcurrentCalls="64" maxConcurrentInstances="1" maxConcurrentSessions="50"
></serviceThrottling>/>
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
If I invoke this reference from a browser window it displays all OK.
It is only where I have a variable in my form class I get the error:
UserControl.Class:
private static wsCloudFeeder.MotionUpdaterClient wsFeeder = new wsCloudFeeder.MotionUpdaterClient();
Server Class:
[ServiceContract]
public interface IMotionUpdater
{
[OperationContract]
void UploadMotion(byte[] jpegStream, string alias, Int16 camIndex);
}
The extra weird thing is that when I run my application it all works no problem.
Also, I have tried just doing this in my control Class but still cannot open up my GUI form..
private static wsCloudFeeder.MotionUpdaterClient wsFeeder = null;
Thanks...
New Error:

Could not find a base address that matches scheme net.tcp for the endpoint

I have a Wcf Service project. system.serviceModel tag in my web.config is :
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="RCISPNetTcpBinding" openTimeout="00:10:00" sendTimeout="00:10:00">
<security mode="Transport">
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign">
</transport>
<message clientCredentialType="Windows"/>
</security>
</binding>
</netTcpBinding>
</bindings>
<services>
<service behaviorConfiguration="RCISP.WcfServiceBehaviour" name="RCISP.WcfService.PersonWcfService">
<endpoint address="PersonServices" binding="netTcpBinding" bindingConfiguration="RCISPNetTcpBinding"
contract="RCISP.Common.ServiceContract.IPersonService" >
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:40003/"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="RCISP.WcfServiceBehaviour">
<serviceMetadata httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false" />
<serviceAuthorization principalPermissionMode="UseWindowsGroups" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
I have a runtime error when I want create self-hosting for service.
public class ServiceFactory : ServiceHostFactory
{
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
if (!IsInitialised) InitialiseService();
return base.CreateServiceHost(serviceType, baseAddresses);
}
}
Message of Exception is :
Could not find a base address that matches scheme net.tcp for the endpoint with binding NetTcpBinding. Registered base address schemes are [http].
Why?
You said you were self hosting the service (within your own process) but based on the code above I think you're trying to host it inside of a web application. If so Visual Studio's web server can not host a net.tcp end point. You'll need to set the project up to run under IIS. Please follow the instructions here .
If you are truly self hosting than you can leave the configuration exactly how you have it and spin up the service using the following code:
var host = new ServiceHost(typeof(Service1));
host.Open();
You need to add a protocol mapping to your configuration file to tell it what the "net.tcp" protocol is. Right after your <bindings> section add this:
<protocolMapping>
<add scheme="net.tcp" binding="netTcpbinding"/>
</protocolMapping>

Categories

Resources