WCF Service Hosted in a Managed Windows Service - c#

I followed this(http://msdn.microsoft.com/en-us/library/ms733069.aspx) link and created a service and and a service host.
I added a webform client project to the solution. In order to check that my service is receiving a request I added a log in the service.
I selected my host and client to run at the same time by setting multiple start up project.
But I am having a problem making a communication between my service and client.Am i missing something in the configuration? i don't see exception at all(even though I selected CLR and JSRuntime exception, and managed debugging assistance ).
Here is my service configuration
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<client/>
<behaviors>
<serviceBehaviors>
<behavior name="meta">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="InboundMessage.Service.Operator" behaviorConfiguration="meta" >
<endpoint address="basic" binding="basicHttpBinding" contract="InboundMessage.Service.IOperator" name="basic"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<!--<endpoint address="" binding="wsHttpBinding" contract="IMetadataExchange" name="Ws" />-->
<host>
<baseAddresses>
<add baseAddress = "http://IP/InboundMessage.Service/"/>
</baseAddresses>
</host>
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="InboundMessage.Service.Operator"/>
</basicHttpBinding>
</bindings>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
Service Host:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="meta">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<diagnostics performanceCounters="ServiceOnly" />
<services>
<service name="InboundMessage.Service.Operator" behaviorConfiguration="meta">
<endpoint address="basic" binding="basicHttpBinding" contract="InboundMessage.Service.IOperator" name="basic"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<!--<endpoint address="" binding="wsHttpBinding" contract="IMetadataExchange" />-->
</service>
</services>
</system.serviceModel>
<system.web>
<compilation
debug="true" >
</compilation>
</system.web>
<system.webServer>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
a windowform Client configuration:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<system.web>
<compilation debug="true"></compilation>
</system.web>
<system.serviceModel>
<services>
<service behaviorConfiguration="meta" name="InboundMessage.Service.Operator">
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="http://IP/InboundMessage.Service/"/>
</baseAddresses>
</host>
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="InboundMessage.Service.Operator"/>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="meta">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
EDIT:
Used Tim's comment to install the service but I am having problem installing it.
I opened another question thanks Tim i am having problem installing the service on my local machine. I opened another question :Unable to install service using sc command

A few of things come to mind.
First (I'm not 100% sure, but this is based on my experiences) you can't run a Windows Service as a Windows Service through Visual Studio. You need to build the project and then install it, as directed on the page you linked to.
Secondly, you only need two configuration files, not three - one for the Windows Service (which is where the configuration for the service goes) and one for the client. I'm not sure what role you have (or believe you have) for the service host config file.
Third, your client config has entries for a service in the <system.serviceModel> section - you only need those if your client is also hosting a service, which doesn't appear to be the case in the question you've asked. You should remove the <services> section and add a <client> section, like this:
<client>
<endpoint address="http://IP/InboundMessage.Service"
binding="basicHttpBinding"
bindingConfiguration="InboundMessage.Service.Operator"
contract="InboundMessage.Service.IOperator" />
</client>
Note that I used the bindingConfiguration attribute above - without that, your client will use the default basicHttpBinding (which in your case won't matter because you didn't set anything other than the name, but if you had set non-default values you would want to tell the client which binding configuration to use).
In reality the simplest way (to get started) would be to build the Windows Service, install it and start it, and then add a service reference to it in your client (WinForm) application. That will generate everything you need and you can take a look at the config file to see the settings you need.

Related

WCF ContractFilter Mismatch when enabling Reliable Session

I have a WCF service hosted in a Windows Service. I then have a GUI(client) that then communicates to this service. It has recently been reported that communication with the service stops after being idle for 10 minutes.
I have done a bit of research and it looks like the service is discarding the connection due to inactivity. Therefore I want to increase the receive timeout and enable reliable sessions and set an inactivityTimeout to be longer. However when I do this in both the WCF service and clients app.config file I get the following error:
Setting reliableSession enabled="False" causes the client and service to run. ( although only for 10 minutes )
Doing some research the suggestion is this is because of one of the following three reasons:
You have different contracts between client and sender.
You're using a different binding between client and sender.
The message security settings are not consistent between client and sender.
However as far as I can tell the settings / contracts between the client and server are consistent. I'm hoping it's something stupid. Here are my app.config for service and client:
Service
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" />
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_IHwResourceManagerWcfService" receiveTimeout="infinite" >
<reliableSession enabled="True" inactivityTimeout="infinite"/>
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost:8523/HwResourceManagerWcfService"
binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IHwResourceManagerWcfService"
contract="WindowsServices.IHwResourceManagerWcfService" name="NetTcpBinding_IHwResourceManagerWcfService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
<services>
<service name="MT.Tools.HwResourceManager.WCF.HwResourceManagerWcfService">
<endpoint address="" binding="netTcpBinding" bindingConfiguration=""
contract="MT.Tools.HwResourceManager.WCF.IHwResourceManagerWcfService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8523/HwResourceManagerWcfService" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
Client
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_IHwResourceManagerWcfService" receiveTimeout="infinite" >
<reliableSession enabled="True" inactivityTimeout="infinite"/>
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost:8523/HwResourceManagerWcfService"
binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IHwResourceManagerWcfService"
contract="WindowsServices.IHwResourceManagerWcfService" name="NetTcpBinding_IHwResourceManagerWcfService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
Any help would be greatly appreciated.
There are several issues in your configuration. This project seems to be a class library project. Please use the WCF service application template. Then the base address of the service should be configured in IIS, not in the configuration file. In addition, your binding configuration will not take effect because you don’t apply it in the service endpoint.
<endpoint address="" binding="netTcpBinding" bindingConfiguration=""
Please refer to my example.
Server (WCF service application).
IService.
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(string value);
}
Service1.svc
public class Service1 : IService1
{
public string GetData(string value)
{
return DateTime.Now.ToLongTimeString();
}
}
Web.config
<system.serviceModel>
<services>
<service name="WcfService3.Service1">
<endpoint address="" binding="netTcpBinding"
contract="WcfService3.IService1">
</endpoint>
<endpoint address="mex" binding="mexTcpBinding"
contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
Then we deploy the service in the IIS. Before we deploy it, we should enable windows features for net.tcp protocol.
Add the support for the net.tcp protocol on the website.
Then add the site binding.
One more thing we need to pay attention to is ensuring the below service is on running state.
Client. (by adding service reference, the client proxy sends an invocation)
static void Main(string[] args)
{
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
//by default, the nettcpbinding uses windows credential, we should provide server windows account.
client.ClientCredentials.Windows.ClientCredential.UserName = "administrator";
client.ClientCredentials.Windows.ClientCredential.Password = "abcd1234!";
try
{
var result = client.GetData("Hello");
Console.WriteLine(result);
}
catch (Exception)
{
throw;
}
}
App.config.
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_IService1">
<security>
<transport sslProtocols="None" />
</security>
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://vabqia969vm/Service1.svc" binding="netTcpBinding"
bindingConfiguration="NetTcpBinding_IService1" contract="ServiceReference1.IService1"
name="NetTcpBinding_IService1">
<identity>
<servicePrincipalName value="host/vabqia969VM" />
</identity>
</endpoint>
</client>
</system.serviceModel>
Result.
Feel free to let me know if there is anything I can help with.
After some more reading on this I found both the reason for the error I was seeing and a solution to the connection timeout issue.
The Error - The error was because I had different binding configurations set. By setting to an empty string for both the client and service the error was removed.
The timeout issue - Even with reliable connections enabled and a long timeout for both the inactivity and receive timeouts the 10 minute connection issue remained. I then read a post that suggested that doing a long timeout was the wrong thing to do. Instead it recommended handling the faulted exception and trying to-reconnect.

Cannot add endpoint for WCF service in WinForms

This is my Web.config in WCF service:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="Mobile55.Service1">
<endpoint address="../Service1.svc"
binding="webHttpBinding"
contract="Mobile55.IService1"
behaviorConfiguration="webBehaviour" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webBehaviour">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type, Accept" />
</customHeaders>
</httpProtocol>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
This is the first time I am working with WCF and I cannot add an endpoint for this service in my WinForms application.
I added this service as a reference and I can access the methods, but when I Debug and try to call a method it throws an exception:
Could not find default endpoint element that references contract 'MyService.IService1' 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.
My application app.config file is empty.
I tried the following answers, but didn't work for me:
Could not find default endpoint element
Could not find default endpoint element that references contract - Hosting wcf
WCF Error - Could not find default endpoint element that references contract 'UserService.UserService'
How do I add an endpoint to finally be able to use my service?
Thanks in advance.
EDIT: My app.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="Mobile55.Service1">
<endpoint address=""
binding="webHttpBinding"
contract="Mobile55.IService1"
behaviorConfiguration="webBehaviour" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="webBehaviour">
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<client>
<endpoint address="http://localhost:1008/Service1.svc"
binding="webHttpBinding"
contract="Mobile55.IService1"
behaviorConfiguration="webBehaviour" />
</client>
</system.serviceModel>
</configuration>
Well, the error message specifically says in the client configuration section.
So the error occurs client-side, i.e. from the code where you try to invoke on your service - more is hard to tell without a proper exception/stacktrace and a complete/compilable example that exhibits the behaviour.
Anyway, your configuration file is missing the client section completely.
Add something like this:
<system.serviceModel>
<!-- ... other stuff you have ... -->
<client>
<endpoint address="../Service1.svc"
binding="webHttpBinding"
contract="Mobile55.IService1"
behaviorConfiguration="webBehaviour" />
</client>
</system.serviceModel>
Something like this should do the trick hopefully
<service name="MyService.Service1">
<endpoint address="YourEndPointHere"
binding="webHttpBinding"
contract="MyService.IService1"
behaviorConfiguration="webBehaviour" />
</service>
You can try with this
<service name="Mobile55.Service1">
<endpoint address=""
binding="webHttpBinding"
contract="Mobile55.IService1"
behaviorConfiguration="webBehaviour" />
</service>
You can call your rest service with like this
localhost:port/Service1.svc/Pathname
Here Pathname is which you mentioned in operationcontract of uritemplate.
Hope it helps!
<system.serviceModel>
<client>
<endpoint address="http://localhost:1008/Service1.svc"
binding="webHttpBinding"
bindingConfiguration="webHttpBinding_IService"
contract="Mobile55.IService1"
name="webHttpBinding_IService" />
</client>
</system.serviceModel>
----------
## Heading ##
Try....

Can't add WCF service reference to client project using netTcpBinding

I have a WCF service that uses NetTcpBinding and I'd like to host it in a WPF application. The service seems to start correctly, but when I'm trying to get it's metadata using 'Add service reference' in visual studio I get this exception:
The URI prefix is not recognized.
Metadata contains a reference that cannot be resolved: 'net.tcp://localhost:8000/Mandrake/mex'.
My service project's App.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" />
</system.web>
<system.serviceModel>
<services>
<service name="Mandrake.Service.OTAwareService">
<endpoint address="OTService" binding="netTcpBinding" contract="Mandrake.Service.IOTAwareService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint name="MEX" address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8000/Mandrake/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
And the code in the hosting application:
Uri baseAddress = new Uri("net.tcp://localhost:8000/Mandrake");
ServiceHost host = new ServiceHost(typeof(OTAwareService), baseAddress);
try
{
host.AddServiceEndpoint(typeof(IOTAwareService), new NetTcpBinding(), "OTService");
}
catch (CommunicationException e)
{
Console.WriteLine(e.Message);
host.Abort();
}
The solutions I found to the problem were mainly about adding the 'serviceMetaData' to the service config or providing a mex endpoint. Could you suggest something?
Edit:
Final config:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="NewBehavior0">
<serviceMetadata />
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="Mandrake.Service.OTAwareService" behaviorConfiguration="NewBehavior0">
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8036/OTService"/>
</baseAddresses>
</host>
<endpoint address="" binding="netTcpBinding" name="TcpEndpoint" contract="Mandrake.Service.IOTAwareService" />
<endpoint address="mex" binding="mexTcpBinding" name="MetadataEndpoint" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
Hosting application:
host = new ServiceHost(typeof(OTAwareService));
host.Open();
I've managed to figure it out, after enabling the serviceDebug's includeExceptionDetailInFaults it was pretty clear.
Mandrake.Service.IOTCallback.Send operation references a message element [http://tempuri.org/:Send] that has already been exported from the Mandrake.Service.IOTAwareService.Send operation
So there was a Send(OTMessage) operation in the service contract and in the callback interface as well. A rather ugly mistake but I thought I would leave the solution here in case it helps anyone.

The operation cannot be completed because the pipe was closed. This may have been caused by the application on the other end of the pipe exiting

I have WCF application and I added a client to consume the service. there is another service written by another developer that sends message to my service.
My service is hosted locally and opened port for access. my service acceps the request from the other service and logs the message.
But I am getting this error : The operation cannot be completed because the pipe was closed. This may have been caused by the application on the other end of the pipe exiting.
My service is installed locally and is started.
I tried telenet but it also throws error : Connection to host lost...
Service class
namespace InboundMessage.Service;
Operator: IOperator {
}
namespace InboundMessage.Service;
IOperator {
}
App.config
<configuration>
<system.web>
<compilation debug="true">
</compilation>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="meta">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="InboundMessage.Service.Operator" behaviorConfiguration="meta" >
<endpoint address="basic" binding="basicHttpBinding" contract="InboundMessage.Service.IOperator"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress = "http://X:Port/InboundMessage.Service/"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
Client
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="InboundMessage.Service" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://X:port/InboundMessage.Service/"
binding="basicHttpBinding" bindingConfiguration="InboundMessage.Service"
contract="IMetadataExchange" name="InboundMessage.Service" />
</client>
</system.serviceModel>
</configuration>

Using wsHttpBinding, WCF is ignoring my web.config

I am trying to create a WCF service that requires session support so I added a section in my web.config to enable wsHttpBinding. But when I test the service in the WCF Test Client and check the config it seems to have taken the default auto generated config instead of my own.
See my config :
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="UserService">
<endpoint address="" binding="wsHttpBinding" contract="ICenterPlaceUserService" />
</service>
</services>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
And the result :
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_ICenterPlaceUserService" sendTimeout="00:05:00" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:57418/L1.WCF/CenterPlaceUserService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ICenterPlaceUserService"
contract="ICenterPlaceUserService" name="BasicHttpBinding_ICenterPlaceUserService" />
</client>
</system.serviceModel>
What am I missing ?
The name attribute of the <service> attribute must have the fully-qualified name of the service class. Otherwise it will be ignored, and you'll see the behavior you're seeing: the default binding is used for the service (basicHttpBinding).
<services>
<service name="TheNamespaceOfYourClass.UserService">
<endpoint address=""
binding="wsHttpBinding"
contract="TheNamespaceOfYourContract.ICenterPlaceUserService" />
</service>
</services>

Categories