WCF Service Application Question - wcf

I am trying to create a WCF application that will be hosted in a Windows Service
I have two options in Visual Web Developer
1) New Website -> WCF Service
2) New Project -> WCF Service Application
Which will be suitable for creating service that can be hosted in windows service? Is there any tutorial explaining hosting in windows service using any of the above mentioned methods?
Thanks
Lijo

In most cases what you'll actually want to do is create two projects; first a WCF Service Library (that's under the WCF templates) and then a Windows Service (which is under the Windows templates), then reference the WCF Service Library from your Windows Service.
The WCF Service Library holds all of your WCF-specific classes - contracts, services, etc. - then you just create a ServiceHost inside the Windows service. This last part requires very little code:
public class MyService : ServiceBase // Windows Service class
{
private ServiceHost host;
protected override void OnStart(string[] args)
{
host = new ServiceHost(typeof(MyWcfLibrary.MyWcfService));
host.Open();
}
protected override void OnStop()
{
host.Close();
}
}
There's a more detailed tutorial here.

As for creating your service - if you already plan to use a NT Service as your host, just use a class library and then add a WCF service to that. Neither of the two options given will give you something suitable for using in a NT Service.
For hosting, see:
MSDN: Host a WCF Service in a managed application
Hosting WCF Services

Lijo,
This page in the MSDN help shows you how to create the WCF service by starting with a Console application. I was able to get everything done with this as a starting point.

Related

WCF Communication in netstandard2.0 (IServiceBehavior and other class which are not supported)

I have a group of services that are written in WCF. This is a whole logic which cannot be changed and rewritten to another technology.
I want to create an application in .NET Core 2.1 and connect to WCF services. I am using library which has clients for all services with custom communication, bindings, endpointbehaviors etc. This library is written in .NET Framework v4.7.2, so if I want to use it in my .NET Core App I need to add target to the .net standard 2.0.
I did this and now I am having a problem as some classes are not supported in .netstandard2.0.
public class UserInfoBehavior : Attribute, IServiceBehavior, IEndpointBehavior
{}
For Example above class is used for adding EndpointBehaviors to my Channel:
public DashboardServiceClient(InstanceContext instanceContext, string endpointConfigurationName, string endpointAddress)
: base(instanceContext, endpointConfigurationName, endpointAddress)
{
base.ChannelFactory.Endpoint.EndpointBehaviors.Add(new UserInfoBehavior());
}
The problem is that IServiceBehavior is not available in .netstandard 2.0. Do you know some equivalent for this? There are more classes which I am using and are not supported:
RemoteEndpointMessageProperty,
Method Open() which is in ClientBase,
ChannelDispatcher,
and also EndpointDispatcher which is empty class in System.ServiceModel.Dispatcher
I am wondering if it is possible to target it to netstandard
You can call the wcf service by adding a connected reference.
Select the WCF Web Service Reference Provider.
Enter your WCF Service URI:
Check all quotes and finish.
You can see this folder in the client.
Just call it in the client, and you can acess the method in WCF Service.
static void Main(string[] args)
{
ServiceReference2.Service1Client service1Client = new ServiceReference2.Service1Client();
var t=service1Client.GetDataAsync(15);
Console.WriteLine(t.Result);
Console.ReadKey();
}
You can also refer to this link to call the wcf service:
Use the WCF Web Service Reference Provider Tool

[Web API]Add Web App to WCF Service solution

I have an issue related with WCF Service and Web API with below steps.
Create a WCF Service Application which is Service1.svc
Right Click this project, and Add -> Web API -> Web API Controller Class
Could I host Web API in WCF solution like this? I did not find any place to configure or register Web API, so if I run this project, Web API will not run correctly.
Code For hosting web api
public class Service1 : IService1
{
public Service1()
{
var config = new HttpSelfHostConfiguration("http://localhost:8080");
config.Routes.MapHttpRoute(
"API Default", "api/{controller}/{id}",
new { id = RouteParameter.Optional });
using (HttpSelfHostServer server = new HttpSelfHostServer(config))
{
server.OpenAsync().Wait();
Console.WriteLine("Press Enter to quit.");
Console.ReadLine();
}
}
No, you can't add a web API controller into a WCF service project and expect it to work. WCF and WebAPI have almost no overlap in terms of their dependency on the BCL.
I think Prashant, in his comment, puts it very eloquently when he argues:
why you want to do this ?
You can host both a WebAPI project and a WCF service project in the same solution, but even though you can do this, why would you want to?
If you want to expose the same endpoints over both SOAP and REST you can already do this using only WCF.

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.

WCF service hosted in Windows service

I have a WCF service hosted in a Windows service. When user modifies the WCF service configuration,he needs to restart the service.
I wanted to know if restarting the windows service is better by using
serviceController.stop()
servicecontroller.start()
or by creating a new instance of the WCF client every time he wants to restart it. No information will be lost if created a new instance of the WCF client.
In your service container which is inherited from System.ServiceProcess.ServiceBase
you should start your service inside method
protected override void OnStart(string[] args)
{
servicecontroller.start()
}
and stop your services inside method
protected override void OnStop()
{
//here clean up code or any tear-down necessary to stop your service.
serviceController.stop()
}
so these methods are called automatically when you start/stop windows service from services pallet.
As others said there is no effect of creating a new instance of the WCF client every time on your service

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