I noticed I cannot add a WCF Data Service (.svc file) to a WCF Service Library project in Visual Studio 2010. I'm a bit of a newb to WCF and was wondering how/why I should put my WCF Data Service into its own assembly.
A service library is a library of services that a host can reference and startup. A service application includes a website host already setup for you.
The WCF service library project alleviates this limitation by providing an App. config file for the library during development. However, the App. config file is not recognized after deployment. You have to move your configuration code into the configuration file recognized by your hosting environment.
Microsoft has stopped developing it, it lacks the features of a . NET Core technology, and it is possible that WCF is not the best framework for the job anymore.
A windows service is what you need. WCF is a communications library, and unless you plan to communicate with your application via a client, you don't need it. Your problem is related to activation, and keeping your code active in the background is what windows services do.
Yes, you can host a WCF Data Service in your own assembly - with a few little tricks. Doing so makes your solution cleaner - it separates the various pieces into more manageable bits, so I would definitely recommend doing this.
Here's how:
put your data model (EF Data Model) into its own assembly, let's call it DataModel
create a new class library project (call it MyDataServiceHost
)
add a few references:
DataModel
assembly with the data layerSystem.ServiceModel
System.ServiceModel.Web
System.Data.Services.Client
System.Data.Services
- you cannot pick this from the usual Add Reference
dialog under the .NET category - you need to browse for the assembly file. Find the directory C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0
(or C:\Program Files (x86)\...
on a 64-bit machine) and pick the System.Data.Services.dll
inside itadd a new class to that class library and call it e.g. YourDataService.cs
- it will look something like this:
using System.Data.Services;
using System.Data.Services.Common;
using DataModel;
namespace MyDataServiceHost
{
public class YourDataService : DataService<YourModelEntities>
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
{
// TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
// Examples:
config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}
}
}
You can name the class anything you like, and it has to derive from DataService<T>
where T
is the name of your data model; if you're using Entity Framework, it's the name of your object context class - typically something like (database)Entities
or whatever you picked when you created the EDM
add another class to your new project, call it MyDataServiceHost.cs
and it will look something like this:
using System;
using System.Data.Services;
using DataModel;
namespace MyDataServiceHost
{
public class MyDataServiceHost
{
public static void LaunchDataService(string baseAddress)
{
Uri[] baseAddresses = new Uri[1];
baseAddresses[0] = new Uri(baseAddress);
using(DataServiceHost host = new DataServiceHost(typeof(YourDataService), baseAddresses))
{
host.Open();
Console.WriteLine("DataService up and running.....");
Console.ReadLine();
host.Close();
}
}
}
}
It instantiates a DataServiceHost, which is derived from WebServiceHost (which in turn is derived from ServiceHost) and it will spin up the WCF Data Service runtime for you.
now you can start up your WCF Data Service from any app using:
MyDataServiceHost.LaunchDataService("http://localhost:4444/YourService");
last thing to remember: the app that you use to launch the WCF Data Service must have the connection string (the EDM connection string, if you're using Entity Framework) in its app.config
(or web.config
) in order for this to work!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With