我是wcf服务的新手。还在读书以获取知识。我的公司在我们的城市有许多办事处,但有一个总部。我的公司有一个静态IP。我的公司计划在HQ服务器上托管一个wcf服务,可以通过互联网通过静态IP到达,其他办公室通过VPN相互连接。我们公司希望我开发一个wcf服务,该服务将用于处理总部所有电脑公司以及所有其他办公室的公司数据。外面的人也可以连接到那个wcf服务。
公司希望当任何人尝试从HQ办公室连接到我们的服务时,HQ lan将用于连接到该服务。
当任何人尝试从我们的其他办公室连接到我们的服务时,将使用WAN或VPN连接。
当任何人尝试从其他地方或家中连接到我们的服务时,将通过互联网进行连接。
我是WCF的新手,因此无法考虑如何使用WCF设计此类服务,WCF将托管在具有静态IP的HQ服务器中。
所以指导我在编码或配置文件中是否有任何调整,我们指定了各种绑定。
我想我们需要在服务配置文件中指定各种绑定。所以请指导我如何设计或编写服务端的配置文件,可以通过LAN,VPN和互联网请求。如果可能的话,给我一份服务端配置文件的样本副本。感谢
答案 0 :(得分:1)
LAN与WAN与VPN相比,WCF的新旧级别太低了。假设您使用say basicHttpBinding并在具有静态IP的HQ服务器中运行的IIS中托管您的WCF服务,您的Internet用户将能够使用外部IP(静态IP)或域名(如果存在)来访问您的服务是一个。对于Intranet(LAN,WAN等),用户可以使用内部IP,您可以通过在网络中ping服务器来获取内部IP。再次假设将使用WCF的计算机与运行WCF服务的计算机之间的路径不会跨越防火墙和内容,您可以使用netTcp绑定,如果您的组织有大量红色,则可以使用更高性能但不值得的麻烦如果中间有防火墙,则磁带打开端口和东西。通常,80和443不会被阻止。
答案 1 :(得分:1)
我们正在开展一个大型项目,我相信这与您正在进行的工作类似。我们有许多用户将从他们的台式PC以及他们的移动设备访问此应用程序。我设计了一个非常灵活的服务层,可以根据用户是本地用户还是远程用户提供最佳性能(请注意VPN =本地)。 由于空间不足,我不能给你所有的细节,但这里有大块的东西:
创建三个Visual Studio项目(或一个包含三个项目的解决方案):1)您的应用程序,2)服务项目(.dll),3)WCF项目。
您的服务项目就是行动所在的位置。 在您的服务项目中,创建一个名为IMyServices的接口(这是标准的WCF内容):
[ServiceContract]
public interface IMyServices : IDisposable
{
[OperationContract]
IEnumerable<Allocation> GetAllocations();
}
接下来,添加一个类,如下所示。我将其称为ServiceRouter,因为如果用户是远程的,它会将请求路由到WCF ,但如果用户是本地用户,则只需通过LAN使用ADO获取数据。请注意,此类包含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服务,如下所示。请注意,此类继承自ServiceRouter,后者实现IMyService。 下面的代码是WCF项目中唯一的代码!所有这些代码都是创建ServiceRouter的一个实例,并传递一个null uri,告诉它通过LAN获取数据。您的WCF服务器和您的数据库服务器需要能够通过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的create实例将null传递给构造函数。如果用户是远程用户,则在创建服务路由器的insance时传递wcf服务器的URI:即ServiceRouter router = new ServiceRouter(myServerName);
答案 2 :(得分:0)
这是一个简单的网络配置问题。您的服务有一个或多个端点。如此简单地将来自各种网络的请求路由到它们。
答案 3 :(得分:0)
你的问题有点“tl; dr”并且是开放式的,但我可以建议你学习"ABC's" WCF端点:地址,绑定,合同吗? 如果您希望将HTTP和TCP同时用于单个服务端点,则可以将两种绑定类型配置到同一端点。
来自文章:
重要的是要注意这三个要素是独立的。 合同可以支持许多绑定,绑定可以支持 许多合同。服务可以有许多端点(合同约束 地址)同时共存和可用。所以,如果你想 通过HTTP公开您的服务并最大限度地使用SOAP 1.1 互操作性,也希望使用二进制文件通过TCP公开它 线编码为最大性能,两个产生的端点可以 并列在同一服务之上。