WCF data service hosting in Windows Form - wcf

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 });

Related

Need to understand the concept from a book

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.

How to secure WCF web services hosted from a Windows Service?

I have a one question about WCF web service security.Currently, we are developing one android mobile project and using wcf web service for data transfer and manipulation.
We use basicHttpBinding and hosted the web service as Window Service.
We don't have any security mode at the moment and I am afraid of every one can consume our web service if they know the service address.
For example, we have one service method and that will return string value. Currently, I can add that service to other visual studio project and mobile project and we can consume any time.
//WCF Service Method
public string DoWork()
{
return "This is return string!";
}
//We can consume it like below from other dot net project by adding service reference.
//Actually, those are not real client.
ServiceReference1.WebServiceClient serv = new TestingPrj.ServiceReference1.WebServiceClient();
string result = serv.DoWork();
My question is how can I secure my web service for real clients? I don't want other projects and people to consume our web services.
The simplest method is to use Basic Authentication over SSL. Basic Authentication requires the client to have a username/password pair, which only your application will know. If the authentication is purely to know if your client is the right one (rather than knowing which user is connecting), then you can use a single, hard-coded username/password.
SSL should be used as well so the credentials don't travel the wire in plain-text and can potentially be sniffed.
Using ssl certificates is the most secure way.

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: Web Service or not?

I'm at the moment creating an ASP.NET web app which should call a WCF Service... The WCF Service have 2 methods:
Bitmap TakeScreenDump(string websiteUrl, int width, int height);
Bitmap GenerateThumbNail(Bitmap source, int width, int height);
The purpose of my ASP.NET web app is to let a user request a screenshot for a specific website, which the web app will hand over to the WCF Service.
Now is my question: Should I create the WCF Service as a Web service (http binding) ? or should I create it as a Console service (net.tcp)? My WCF Service wont be used by anyone else than me.
Whats the advantages/disadvantages of those two in this case?
It doesn't matter. The service code will be exactly the same.
It's one of the most important things about WCF that the service is separate from the binding. If you think you might want to use more than one binding, then create the service as a "WCF Service Library" project. Then you can host it in a Console Application, Windows Service, or whatever, using whichever binding you like.
In fact, there's no reason you can't do both. You can host it in a Console application using one of the HTTP bindings if you like.

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.