我们现在正在开展一个大型项目,我相信这与您正在从事的工作相似。我们有许多用户将从他们的桌面 PC 以及他们的移动设备访问这个应用程序。我设计了一个非常灵活的服务层,它根据用户是本地用户还是远程用户(注意 VPN = 本地)提供最佳性能。由于空间不足,我无法为您提供所有细节,但这里有一些重要的部分:
创建三个 Visual Studio 项目(或一个包含三个项目的解决方案):1) 您的应用程序,2) 服务项目 (.dll),3) WCF 项目。
您的服务项目就是行动所在。在您的服务项目中,创建一个名为 IMyServices 的接口(这是标准的 WCF 内容):
[ServiceContract]
public interface IMyServices : IDisposable
{
[OperationContract]
IEnumerable<Allocation> GetAllocations();
}
接下来,添加一个如下所示的类。我称它为 ServiceRouter 是因为如果用户是远程用户,它会将请求路由到 WCF ,但如果用户是本地用户,它只会使用 ADO over LAN 获取数据。请注意,此类实现 IMyServices。
public class ServiceRouter : IMyServices
{
private readonly string ServiceURI;
/// <summary>
/// Routes data requests over the LAN if the client is connected locally or over a WCF service if the client is remote. Use this constructor to route data requests over the LAN.
/// </summary>
/// http://msdn.microsoft.com/en-us/library/ms735103.aspx
///
public ServiceRouter()
{
ServiceURI = null;
}
/// <summary>
/// Routes data requests over the LAN if the client is connected locally or over a WCF service if the client is remote.
/// </summary>
/// <param name="serviceURI">Fully qualified URI of a WCF server if the user is remote. Pass null if the user authenticated on the LAN (including using VPN)</param>
/// http://msdn.microsoft.com/en-us/library/ms735103.aspx
///
public ServiceRouter(string serviceURI)
{
ServiceURI = serviceURI;
}
public IEnumerable<Allocation> GetAllocations()
{
IMyServices client = GetClient();
var result = client.GetAllocations().ToList();
CloseClient(client);
return result;
}
#region WCFClient
private IMyServices GetClient()
{
IMyServices _client;
if (string.IsNullOrEmpty(ServiceURI))
_client = new MYServices();
else
{
_client = ChannelFactory<IMyServices>.CreateChannel(new BasicHttpBinding(), new EndpointAddress(ServiceURI));
((ICommunicationObject)_client).Open();
}
return _client;
}
private void CloseClient(IMyServices client)
{
ICommunicationObject wcfClient = client as ICommunicationObject;
if (wcfClient != null)
{
if (wcfClient.State == CommunicationState.Faulted)
wcfClient.Abort();
else
wcfClient.Close();
}
else
((IDisposable)client).Dispose();
}
#endregion
}
接下来,在您的服务项目中,为您的服务创建一个实现 IMyServices 的类,如下所示:
internal partial class MyServices : IMyServices
{
public IEnumerable<Allocation> GetAllocations()
{
// access your db here
}
下面介绍如何使用 WCF 公开服务。您将需要配置您的 web.config,并且您需要从您的服务项目中引用 .dll 文件。
在您的 WCF 项目中添加一个 WCF 服务,如下所示。请注意,该类继承自实现 IMyService 的 ServiceRouter。 下面的代码是 WCF 项目中唯一的代码! 这段代码所做的只是创建一个 ServiceRouter 的实例,向它传递一个空 uri,告诉它通过 LAN 获取数据。当然,您的 WCF 服务器和 DB 服务器需要能够通过 LAN 进行通信才能正常工作。
public class MyWCFService : MyServiceProject.ServiceRouter
{
public MyWCFService() : base()
{
// Since the WCF server is running on the local area network, this class only needs to create an instance of
// the service router in local mode and retrive the requested data. WCF serializes the data and sends it
// back over the wire.
}
}
这是您的 web.config 外观的片段:
<service name="MyWCFService" behaviorConfiguration="xxx">
<endpoint address="" binding="basicHttpBinding" bindingNamespace="http://blah.com" contract="MyServiceProject.IMyServices">
在您的应用项目中,添加对您的服务 .dll 文件的引用。查看您的用户的 IP 地址,如果它是本地的,请使用创建 ServiceRouter 的实例,将 null 传递给构造函数。如果用户是远程用户,请在创建服务路由器实例时传递 wcf 服务器的 URI:即 ServiceRouter router = new ServiceRouter(myServerName);