是否可以在一个WCF服务中托管多个服务合同?如果是这样,怎么样?我一直在谷歌搜索和一些帖子说你可以做(但不是如何)和其他人说,这是不可能的.
当我运行服务器时,我收到此错误:
在服务'ConsoleAppWcfServer.FooService'实现的合同列表中找不到合同名称'ConsoleAppWcfCommon.IBarService'.
这是我的服务器代码:
static void Main(string[] args)
{
string serviceAddress = "net.tcp://localhost:8088/FooBarService";
// I'm stuck here as I have to pick *one* service
ServiceHost selfServiceHost = new ServiceHost(typeof(FooService));
// I can add both endpoints here, but this is what gives me the error.
selfServiceHost.AddServiceEndpoint(typeof(IFooService), new NetTcpBinding(), serviceAddress);
selfServiceHost.AddServiceEndpoint(typeof(IBarService), new NetTcpBinding(), serviceAddress);
selfServiceHost.Open();
Console.ReadLine();
selfServiceHost.Close();
}
Run Code Online (Sandbox Code Playgroud)
这是客户端代码:
static void Main(string[] args)
{
NetTcpBinding netTcpBinding = new NetTcpBinding();
EndpointAddress endpointAddress = new EndpointAddress("net.tcp://localhost:8088/FooBarService");
// Call IFooService
var channelFactoryFoo = new ChannelFactory<IFooService>(netTcpBinding, …Run Code Online (Sandbox Code Playgroud) 我正在编写一个需要托管多个WCF服务的应用程序.WCF的优势之一是能够通过在app.config文件中指定设置来配置服务而无需重新编译.
在自托管时,似乎没有一种开箱即用的方式来自动托管app.config文件中的服务.我发现这个问题提到了一个可能的解决方案,即在运行时动态枚举app.config中列出的服务,并为每个服务创建一个ServiceHost.
但是,我的服务,合同和托管应用程序都在不同的程序集中.这导致Type.GetType(string name)无法找到我的服务类型(返回null),因为它是在不同的程序集中定义的.
如何可靠地托管app.config文件中列出的所有服务(即,new ServiceHost(typeof(MyService))在我的自托管应用程序中没有硬编码?
注意:我的app.config是使用Visual Studio 2010中的"WCF配置编辑器"生成的.
另请注意:我的主要目标是由app.config文件驱动,因此只有一个配置点.我不想在一个单独的位置配置它.
编辑:我能够读取app.config文件(请参阅此处),但需要能够解析不同程序集中的类型.
编辑:下面的答案之一促使我尝试在app.config中指定AssemblyQualifiedName而不仅仅是基本类型名称.这能够解决Type.GetType()问题,但无论我如何获得类型,ServiceHost.Open()现在都失败了InvalidOperationException:
// Fails
string typeName = typeof(MyService).AssemblyQualifiedName;
Type myType = Type.GetType(typeName);
ServiceHost host = new ServiceHost(myType);
host.Open(); // throws InvalidOperationException
// Also fails
Type myType2 = typeof(MyService);
ServiceHost host2 = new ServiceHost(myType2);
host2.Open(); // throws InvalidOperationException
Run Code Online (Sandbox Code Playgroud)
例外细节:
Service 'SO.Example.MyService' has zero application (non-infrastructure) endpoints. This might be because no configuration file …
如何编写具有单个端点但具有多个服务合同的WCF Web服务?
例:
[ServiceContract]
public interface IWirelessService
{
[OperationContract]
void AddWireless();
}
[ServiceContract]
public interface IWiredService
{
[OperationContract]
void AddWired();
}
[ServiceContract]
public interface IInternetService
{
[OperationContract]
void AddInternet();
}
Run Code Online (Sandbox Code Playgroud)
让我们认为IInternetService是我的主要Web服务,我想在其中实现IwiredService和IWirelessService,但我想在他们的类中实现.这可能吗?我怎么解决这个问题?
我在Windows服务中托管了一个WCF进程.我想知道我是否可以安全地拥有多个WCF进程,这些进程在同一个Windows服务中托管不同的东西.我不得不担心端口吗?我正在使用mex端点