Wcf Push Service in Windows Store App - c#

I´m trying to implement a Wcf Duplex Service in my Windows Store Application. In my .NET WPF Application everything works fine. I use a NetTcpBinding.
Here is my Service Config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="myNetTcpBinding" receiveTimeout="00:30:00">
<reliableSession enabled="true" inactivityTimeout="24.20:31:23"/>
</binding>
</netTcpBinding>
</bindings>
<services>
<service name="CnS.Base.Services.MessageHub.MessageHubService">
<endpoint address="" binding="netTcpBinding" bindingConfiguration="myNetTcpBinding" contract="CnS.Base.Services.MessageHub.IMessageHubService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexTcpBinding" bindingConfiguration="" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress = "net.tcp://localhost:8655/MessageHubService" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="False" httpsGetEnabled="False"/>
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
If i call a Method from the WCF Service i get a ActionNotSupportedException.I figured out that if i set
<reliableSession enabled=false"/>
It works fine. But my Problem is now that the Wcf connection is closed to my Windows Store App and i can´t Push Messages to this client.
Does anyone know a workaround for this Problem?

Related

How to configure self hosted WCF service to compress messages

I've been looking online but I can't find a defined procedure or an example on how to configure a basicHTTP self-hosted WCF service to compress messages that servers sends to clients.
I would like to use GZIP or similar technology.
this is my application app.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<services>
<service name="Application.ApplicationServicesProxy" behaviorConfiguration="ApplicationSvcsBehave">
<host>
<baseAddresses>
<add baseAddress="http://localhost:9001/ApplicationSvcs"/>
</baseAddresses>
</host>
<endpoint address="http://localhost:9001/ApplicationSvcs" binding="basicHttpBinding" contract="Application.IApplicationServices"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ApplicationSvcsBehave">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="True"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
</startup>
</configuration>
This is how I create the service:
var host = new ServiceHost(typeof(ApplicationServicesProxy));
host.Open();
Services are defined in IApplicationServices that is inherited by ApplicationServicesProxy
[ServiceContract]
interface IApplicationServices
{
[OperationContract]
string[] MethodOne();
}
public class AppicationServicesProxy : IApplicationServices
{
public string[] MethodOne()
{
// return a string value;
}
}
And this is how I connect the client to the service
var ApplicationSvcs = new ApplicationSvcs.ApplicationServicesClient("BasicHttpBinding_IApplicationServices");
I am currently using .NET 4.8
I solved doing like this, message size went down fromm 300 Kb/s to 11 Kb/s so it really works and CPU is not very interested in that:
<system.serviceModel>
<services>
<service name="Application.ApplicationServicesProxy" behaviorConfiguration="ApplicationSvcsBehave">
<host>
<baseAddresses>
<add baseAddress="http://localhost:9001/ApplicationSvcs"/>
</baseAddresses>
</host>
<endpoint address="" binding="customBinding" bindingConfiguration="BinaryCompressionBinding" contract="Application.IApplicationServices"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ApplicationSvcsBehave">
<serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<customBinding>
<binding name="BinaryCompressionBinding">
<binaryMessageEncoding compressionFormat ="GZip"/>
<httpTransport decompressionEnabled="true"/>
</binding>
</customBinding>
</bindings>
</system.serviceModel>
and on client side
<system.serviceModel>
<bindings>
<customBinding>
<binding name="CustomBinding_IApplicationServices" >
<binaryMessageEncoding compressionFormat="GZip"/>
<httpTransport maxReceivedMessageSize="20000000"/>
</binding>
</customBinding>
</bindings>
<client>
<endpoint address="http://localhost:9001/ApplicationSvcs" binding="customBinding"
bindingConfiguration="CustomBinding_IApplicationServices" contract="ApplicationSvcs.IApplicationServices"
name="CustomBinding_IApplicationServices" />
</client>
</system.serviceModel>
SOAP messages can be very heavy but in my application I could not use REST so I hope that can be of any help.

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.

How to host service using UDP in WCF?

I am trying to host a service using UDP in WCF but I can't generate a proxy from the service. I need to host the service on a LAN. Can anyone give an example for a server and client using UDP in WCF?
Here is my app.config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<extensions>
<bindingElementExtensions>
<add name="udp_Transport" type="Microsoft.ServiceModel.Samples.UdpTransportElement, UdpTransport" />
</bindingElementExtensions>
</extensions>
<behaviors>
<serviceBehaviors>
<behavior name="">
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
<serviceMetadata httpGetEnabled="false"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<udpBinding>
<binding name="UDPBinding" openTimeout="24.20:31:23.6470000" receiveTimeout="24.20:31:23.6470000" sendTimeout="24.20:31:23.6470000" maxBufferPoolSize="10000000" maxReceivedMessageSize="10000000">
<readerQuotas maxDepth="10000000" maxStringContentLength="10000000" maxArrayLength="10000000" maxBytesPerRead="10000000" maxNameTableCharCount="10000000"/>
</binding>
</udpBinding>
</bindings>
<services>
<service name="UDP_Server.Service1">
<endpoint address="soap.udp://localhost:40000/Service1/" binding="udpBinding" bindingConfiguration="UDPBinding" contract="UDP_Server.IService1">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<host>
<baseAddresses>
<add baseAddress="soap.udp://localhost:40000/Service1/"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup></configuration>
Check the link below:
https://www.codeproject.com/Articles/757349/Multicasting-using-UdpBinding-in-WCF
It also have a code example.

Using custom tcp bindings

I have a custom tcp binding I want to test on a service, specifically to disable the security:
<bindings>
<netTcpBinding>
<binding name="customTcpBinding" maxReceivedMessageSize="20480000" transferMode="Streamed" >
<security mode="None"></security>
</binding>
</netTcpBinding>
</bindings>
However, upon using the tcp binding's name in the config I recieve this error:
Severity Code Description Project File Line
Warning The 'binding' attribute is invalid - The value 'customTcpBinding' is invalid according to its datatype 'serviceBindingType' - The Enumeration constraint failed. Server C:\Users\Totally Not Beau\documents\visual studio 2015\Projects\WCF Proj\WCF Proj\App.config 24
Endpoint config:
<endpoint
address=""
binding="customTcpBinding"
bindingConfiguration=""
contract="Server.IMyService">
</endpoint>
Whole file if it helps:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<netTcpBinding>
<binding name="customTcpBinding" maxReceivedMessageSize="20480000" transferMode="Streamed" >
<security mode="None"></security>
</binding>
</netTcpBinding>
</bindings>
<services>
<service name="Server.MyService">
<endpoint address="" binding="customTcpBinding" bindingConfiguration=""
contract="Server.IMyService">
</endpoint>
<endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://192.168.2.7:8732/MyService/" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
Any advice would be greatly appreciated!
I think what you want is
<endpoint
address=""
binding="netTcpBinding"
bindingConfiguration="customTcpBinding"
contract="Server.IMyService">
</endpoint>
You may have to specify an actual address too, I'm not positive.

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>

Categories