Need to understand the concept from a book - wcf

I am learning WCF using a book named "Window communication foundation 4: step by step". I the second chapter, there was a tutorial about developing windows service for WCF. The client communicates to the named pipe endpoint.
//WCF inside Windows service.
protected override void OnStart(string[] args)
{
productsServiceHost = new ServiceHost(typeof(ProductsServiceImpl));
NetNamedPipeBinding binding = new NetNamedPipeBinding();
productsServiceHost.AddServiceEndpoint(typeof(IProductsService),
binding, "net.pipe://localhost/ProductsServicePipe");
productsServiceHost.Open();
}
And the client has an endpoint defined in App.config
<endpoint address="net.pipe://localhost/ProductsServicePipe"
binding="netNamedPipeBinding" bindingConfiguration=""
contract="ProductsService.IProductsService"
name="NetNamedPipeBinding_IProductsService" />
I need to create a proxy object to a "Service reference" which is not the windows service I mentioned earlier.
// Create a proxy object and connect to the service
// There service reference for "ProductsServiceClient" is
// "http://localhost:51397/ProductsService/Service.svc"
ProductsServiceClient proxy = new ProductsServiceClient("NetNamedPipeBinding_IProductsService");
Without "ProductsServiceClient", I could not initiate the proxy. Why do I need to that service reference, as I connect to a window service. I could not understand the concept clearly.

In this case the term service is being used in two senses. You have the windows service which is the host for the WCF service. The reason that you had to create a service reference was so that your project could generate all off the code necessary to create a proxy to communicate with the WCF service. As an alternative to adding a service reference you could also have used a Channel Factory. The key thing to understand is that when you are creating a WCF service you generally access that service through the WCF pipeline (via a proxy or a channel factory), even if you are doing so from the project where it is defined.
If it is the case that you are unclear about how to generate a proxy for self hosted WCF service, you should look into how svcutil.exe can used to generate proxies for compiled services.

Related

Consume wcf service in .net core which to use services.AddSingleton or services.AddTransient

I am consuming a WCF service in my .net core API project and I used the following steps:
The binding in WCF service (for testing) <add binding="basicHttpsBinding" scheme="https" />
Created a proxy class using https://learn.microsoft.com/en-us/dotnet/core/additional-tools/dotnet-svcutil-guide?tabs=dotnetsvcutil2x (This created a proxy which has an interface IService1 and calls Service1Client).
In the startup class I have bound the interface with the class services.AddSingleton<IService1, Service1Client>();
The method endpoint in the WCF service gets the data form the database according to the parameter passed to consume the WCF service.
I am not sure which one I should use, services.AddSingleton or services.AddTransient, because I am not sure what the proxy class is using to call the method.
If I create a single instance, will it be locked?
I have done a Jmeter test with 1000 rows in the database and 1000 rows from csv as a parameter to consume the API, but did not find any lock and all were successful under 3 minutes.
You can view the servicereference.cs file to see which method in the wcf service is called by the proxy class.
Then we need to instantiate the proxy class and call the WCF service through the instantiated proxy class:
ServiceReference.Service1Client service1 = new Service1Client();
By this documentation in a web api a single httpclient should be instantiated:
https://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client#create-and-initialize-httpclient
Of course if you have multiple target services, with different endpoints, an HttpClient per service is needed (you set the base address in the HttpClient)
In your case your generated client should use an HttpClient under the covers. If you can determine if the whole genrated client is thread safe then you can inject the client as a Singleton, if not you should add it as scoped

Referencing WCF Services without mex binding

I was wondering how a client project in Visual Studio could reference a WCF service that doesn't have a mex binding. Whenever I remove the default mex binding in any sample WCF service, the client apps cannot find the service and in the auto-generated comments, it's recommended that in production environment, mex binding should be removed. Then how are the client apps supposed to find the service and reference it?
If you have access to the assemblies which contain the types which define the service contract, operations, and data contracts, then you can just create a proxy on the fly using ChannelFactory. In this instance you would not need to retrieve any service metadata as you already have access to all the information you need to call the service.
For example
// Create service proxy on the fly
var factory = new ChannelFactory<IMyServiceContract>("NameOfMyClientEndpointInConfigFile");
var proxy = factory.CreateChannel();
// Create data contract
var requestDataContract = new MyDataContract();
// Call service operation.
var responseDataContract = proxy.MyServiceOperation(requestDataContract);
It also helps if you have access to the service-side config file so you can copy the endpoint details out of there into your client config.
The mex endpoint is a necessary part of WCF SOAP services. It is what enables client toolkits to pull down the WSDL and auto-generate proxy classes. As you point out, without it, clients have no way to get the information to consume the service. If you want clients to be able to consume and find your service, you should leave it available when your service is in production.

WCF data service hosting in Windows Form

I want to host a WCF Data Service (formerly known as ADO.NET data Service) in windows form.Is it possible? If yes,then is there any blog, which talks about it?
I know WCF can be hosted in Windows Form, but I am not sure about WCF data service, as all the examples I see, is asking to create ASP.NET web project.
-Brajesh
It is very easy to host a WCF Data Service in a WinForms application (or in my case a unit test).
// add reference to System.Data.Services
// identify your endpoint uri
Uri endpoint = new Uri("http://localhost:12345/MyDataService");
// create the data service host
DataServiceHost host = new DataServiceHost(typeof(MyDataService), new Uri[] { endpoint });

accessing WCF service through URL

I have a WCF service ( Let's say WCFService1 ) is deployed on two remote machines. Since the same service is deployed on two different machines they have common interface and common methods exposed.
WCFService1 is deployed on Machine1 and Machine2.
To consume WCF service from client machine, I have created a client app:
I have added a design time reference of WCF service (WCFService1 )( with the help of URL http://11.12.25.23/WCFService/Service1.svc).
Now I can invoke the methods exposed in the service. Up until now its fine...
Now my question is If I have to update client at run time with same service hosted in different machine with different URL ( Let's say http://12.12.24.24/WCFService/Service1.svc), How can I do that?
At present I am doing this:
BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress("http://12.12.24.24/WCFService/Service1.svc");
MyServiceClient serviceClient = new MyServiceClient(binding, address);
but whenever I use to invoke the method exposed in the service I got binding mis match error.
Have you tried invoking your client first?
eg:
MyWCFClient client = new MyWCFClient();
client.EndPoint.Address = new EndpointAddress("http://somewhere:888/here.svc");
I'd suspect, that if you look in your web.config file on Machine1, you'll see that the binding there is WSHttpBinding (or something different than BasicHttpBinding). If you change it to BasicHttpBinding (assuming that is what you really want), you'll remove this error.
How is your service configured? Show us your server-side and client-side config!
Binding mismatch means you're either not using the same binding, or some vital parameter on the binding is different - there must be something configured wrong - so show us the config!
Marc

Consume WCF Service Hosted in a Windows Service

I wrote the WCF Service and hosted in windows service. I need to know how to consume this windows service in my client application.
Note:
I wrote Net pipe binding service.
Edit:
How can I write the client application for net pipe binding?
You need to do a few easy steps:
start your Windows service hosting your WCF service
from within Visual Studio (2008 or higher), right-click on a project node in the solution explorer and choose "Add service reference"
enter the URL where your service can be reached
That's about all there is, really. Visual Studio will go to your running service, get all the metadata it needs (assuming you've enabled a MEX endpoint for metadata exchange), and will create a client proxy class for you to use to connect your client to your service.
Marc
you need to use ChannelFactory to create a proxy, and then you can use the proxy to perform wcf tasks.
ChannelFactory<IWCFService> pipeFactory = new ChannelFactory<IWCFService>(
new NetNamedPipeBinding(),
new EndpointAddress("net.pipe://localhost/PipeWCFService"));
IWCFService pipeProxy = pipeFactory.CreateChannel();
pipeProxy.RunWCFServiceMethod();}
http://www.switchonthecode.com/tutorials/wcf-tutorial-basic-interprocess-communication
You can consume it like any other WCF service. The method used for hosting the WCF service is not relevant to the client side.
If you need details on how to actually build the client, let me know and i'll update the post.
Edit : Start here to learn how to build a WCF client.