How to use a WCF Service Library in a solution - c#

I know how to create a restful web service using WCF. If i create the service as a "WCF Service Library" and implement it in a solution, how do i activate it, when it is not the main project?
I am using a N-tiered architecture. The webservice should have access to some layers below it, while clients from the outside should be able to call the webservice.
What is the best way to host a service in my case ? Windows Service? IIS ? Self-hosting?
Thanks guys

Thera are various options to host your wcf service , theory behind each option is detailed here
http://msdn.microsoft.com/en-us/library/ms730158.aspx
It looks like you don't have any hosting code , in that case hosting using iis is your best option as all other options require you to have some hosting code I.e. a main entry point.
For iis you just have to create a web application project as instructed in the iis hosting section in the following article
http://www.codeproject.com/Articles/150066/Create-Host-Self-Hosting-IIS-hosting-and-Consume-W

You can do this in two ways I think:
Visual Studio can host the service for you if you want (It should have set this up automatically)
Create a separate Console project where you host your WCF Service. Then change the solution to have multiple start-up projects, so that you can start both your app and the console for WCF. More info about self hosting here

You can configure a solution to start multiple projects. Here is the MSDN link for this.

Related

Confusion about WCF Class library and WCF application hosting

I have a web site and would like to expose certain functionalities using WCF.
Before deciding which type of WCF project I need to use I wanted to compare the differences between WCF Class library and WCF application. I know this question has been asked many times and answered many times and the answers are usually about different hosting options each one offers but I wanted to try and see the differences, so this is what I tried:
Step 1 - In a same solution, I have created a WCF Service library project and created Client console app project, set the console app as a startup project, referenced the service library project from the client project
and in the client console app I could instantiate the service and can consume the service methods. I didn't even add a service reference to the client project.
Step 2 - In a same solution, I have created a WCF Service application project and created Client console app project, set the console app as a startup project, referenced the service application project from the client project
and in the client console app I could instantiate the service and can consume the service methods. I didn't even add a service reference to the client project.
On both steps after compiling the solutions I was able to copy the client app's exe and the service dll's to a different location and still be able to run the clients.
Based on this little excercie I am confused about the hosting part. It seems wether I use WCF Class library or WCF application type I get the same result.
This is just like using multiple projects in a solution, you reference one from another and use the methods, there must be something I am missing which highlights the differences between the two and highlights the benefits of using WCF, also in the past I remember I had to add a service reference to the clients apps in order to consume the service, why is this not the case here?
Thanks
1) Running a wcf service application allows you to provide communication into a single application, where you have a single instance of a thing you want to provide access to. Maybe this is a game, or a chat room without an external state engine or datastore. This is useful for providing diagnostic information about an application you might have written for example. I used this to provide external control for an industrial robot that I wanted to provide remote control access for.
That is to say, that you write an application, it has a function. You want to expose part of that functionality to remote applications. You do this by adding a WCF endpoint to your existing application, so your application itself is controlling the WCF hosting elements, lifecycle of the endpoint etc.
2) Running a WCF Service is for when you've got an external data store, or your service is stateless. A translation service, lookup service and web page requests fall into this category.
With a service class, you're saying here is this service, this thing that provides a function. It isn't tied to the lifecycle of another application or process and is typically hosted by IIS. IIS manages when the class is loaded and run based on the requests that come into it. These services have no internal persistence and rely on an external datastore, or are, by nature, stateless (think of a postcode lookup, or a calculator service)
It sounds like you're actually adding the projects as references, rather than connecting to them as services. That is to say, that the consuming application is actually loading the service as an assembly (in the same application/ memory space) rather than as a separate application/ service that your application then uses WCF to communicate with.
The WCF Service Application template can be used to create WCF services with a hosting website created within the project
The WCF Service Library template can be used to create WCF services that are hosted by the WCF Service Host, and these can be tested using the WCF service Test Client.
the biggest advantage of using a standalone library (apart from decoupling the logic) is that you can easily migrate your service, i.e. host it in another application or another type of application. E.g., let's say you're hosting your service using IIS - you can easily move your service to a standalone application, etc.

Why Host WCF Service in Worker Role

In the process of building my first AZURE based application using WCF services I have stumbled across a number of examples where people show how one can host a WCF Service inside a Worker Role.
Such as in these articles:
http://www.codeproject.com/Articles/188464/Host-WCF-Services-in-an-Azure-Worker-Role
http://code.msdn.microsoft.com/windowsazure/CSAzureWCFServices-20c7d9c5
Very simple question, can someone please explain what use case would require hosting a WCF service in a worker role? What are the motivation/advantages of doing this?
Hosting a WCF service inside of a WebRole implies that it is hosted within IIS. Some folks prefer to not have the footprint of IIS mess with their ServiceHost and host the service directly. They have more control over how the communication with their service is done without IIS in the middle.
Also, when shrink-wrapping the packaged solution for customers, it is simpler to create an installer package without trying to rely on IIS infrastructure that customers may or may not have properly deployed/configured.
HTH

How to structure WCF web service solution so it would be easy to develop and host in different ways?

I'm about to start building WCF web service, and i would like to structure solution in such way that is would be:
Easy to develop - i would like so that developers on my team could just get sources, build, and start service, probably best would be to have possibility to start it as console app?
Easy to host this service later in IIS, WAS or windows service.
I was thinking about having this projects:
Shared (for web service interface)
WebService (for actual implementation of web service and svc file)
ConsoleHost (for hosting web service in console app)
Will there be any problems with such approach?
What could be things that i should consider in advance?
Maybe there is better structure?
I would appreciate any insights and links to resources that could help me chose right structure.
p.s. web service itself is simple, but this approach would be reused for more services.
What I tend to do is to place all my interface definitions in one assembly/project, all the service implementations in another assembly project, and the hosting environment, whether it be console, IIS (ie the .svc files) or windows service or whatever, in a separate assembly.
With this information, I say this structure looks fine.
You might also consider creating a separate ServiceModel assembly to store all your custom behavior and WCF. They could then be reused for other projects

.net web service hosted within my application

I'm migrating an old Delphi application that I wrote into C#. The application is a datalogger that exposes logged data requests via a SOAP web service interface.
The web service is contained with the delphi graphical windows application, i.e. no need to run a web server like IIS, etc I just run the application and it's up and running under the hood.
I'm looking to do the same in my c# Windows form application, I can find loads of resources on writing web services that are ultimately hosted within IIS but am struggling to find a solution for a self contained web service within my application.
Does anyone have any suggestions or can point me towards any resources on this?
The web service does not neceserily have to be SOAP, REST is fine (in fact probably prefered).
Look into WCF Services.
Hosting and Consuming WCF Services
Hosting WCF services in a Windows Forms Application
The System.Web.Hosting namespace allows you to host ASP.Net pages without using IIS within your applications. I have never used it to host web services but I found a tutorial that seems to provide a guide on doing this-
http://msdn.microsoft.com/en-us/magazine/cc163879.aspx
If you're wanting to host a service inside your application, it's possible with the System.ServiceModel.ServiceHost class. You need to learn WCF first, but that at least answers your question to get you started. If you have any further questions, leave me a comment or two and I'll update my answer to accommodate your inquiries.

Calling ASP.net Web Service from C# Application

I have a question. How can i invoke a web service and get the result from a C# desktop application. I am making a desktop app and I want it to be able to connect to my online ASP.net web services. How is this possible?
In Solution Explorer, right-click your project node and select Add Service Reference.
Enter the URL where your service WSDL is located. This is usually the URL of the service itself.
This generates a strongly-typed proxy class in a new Services References folder in your project.
Write code in your desktop app to instantiate the proxy class and invoke methods on it. The rest works like magic. :)
AB Kolan was also correct, but Add Web Reference uses the old-style web services framework whereas Add Service References uses the new WCF stack. Important note: It is not required that the service itself use WCF for you to use WCF on the client side. WCF on the client is typically the best choice for any service, provided you can take a dependency on .NET 3.0 and above.
Add a Web Reference to the webservice in your Desktop App project reference. Doing so would generate a Proxy for the Webservice called Reference.cs
You can access your webservice using the proxy.
This is possible the same way that you access web services from any other type of application, be it an ASP.NET page, a class library or windows service.
For an explanatory tutorial on the subject, see Accessing a Web Service from a Desktop Application.
Will get help how to create a webservice and consume that service:
http://www.c-sharpcorner.com/UploadFile/0c1bb2/consuming-web-service-in-Asp-Net-web-application/
Thanks

Categories

Resources