New to WCF Web Services
I am new to WCF and I want to know if it is possible to create a WCF service on it's own application and host it on IIS. Then another ASP.NET application will consume it. So far all the examples i have seen have the WCF service inside the same ASP.NET solution. If so, can you please provide some hints about开发者_如何学C how to do it? I want to create a simple example that retrieves the person last name based on the id and then consume it from a separate ASP.NET application.
thank you.
Yes you can have it "standalone" - in Visual Studio start with a WCF application - this application will then be hosted in IIS.
Below very basic from the VS template.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WcfService1
{
// NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file.
public class Service1 : IService1
{
public string GetLastName(int id)
{
//Do Data Access here
string surname = "SomeSurname";
return surname;
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}
}
Yes, absolutely - your WCF service can live in a class library assembly, and hosting it in IIS is peanuts.
Another ASP.NET app can then call it and use its services. No problem at all.
As for resoures: there's the MSDN WCF Developer Center which has everything from beginner's tutorials to articles and sample code.
Also, I would recommend you have a look at the Pluralsight screen casts on WCF - it's an excellent series going from "Creating your first WCF service" and "Creating your first WCF client" all the way to rather advanced topics. Aaron Skonnard very nicely explains everything in 10-15 minutes screencasts - highly recommended!
The answer is yes (koosk and marc_s have good answers) - however the reason for the examples being the way they are deserves a brief comment.
If you look at the examples you will probably see (at least) two projects, one for the sample service and one for the sample app that consumes it, in theory you could pull the projects into separate solutions, fire the service up and then the consuming app and it will still work (in theory because you'd need to get the right dependencies into both projects).
The reason to have both client and service in the same solution is because it enables visual studio to get smart about helping you with your debugging, when you start the client it will automagically start the service for you and you should then be able to step from the client into the service if required.
Its worth remembering that a solution can contain any number of notionally distinct applications as well as the libraries etc required to support those applications, its just a wrapper - an application (service or client in this case) is just another project in the solution.
精彩评论