在我的书中,它希望我使用两个绑定来暴露两个端点:WsHttpBinding& NetTCPBinding并在主机应用程序中托管服务。
我在C#中使用以下代码尝试连接到我的服务:
Uri BaseAddress = new Uri("http://localhost:5640/SService.svc");
Host = new ServiceHost(typeof(SServiceClient), BaseAddress);
ServiceMetadataBehavior Behaviour = new ServiceMetadataBehavior();
Behaviour.HttpGetEnabled = true;
Behaviour.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
Host.Description.Behaviors.Add(Behaviour);
Host.Open();
在服务方面,我有:
[ServiceContract]
public interface IService...
public class SService : IService....
然后在我的Config文件中我有:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="Behavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="WsHttpBehaviour" name="SService.SService">
<endpoint
address=""
binding="wsHttpBinding"
bindingConfiguration="WsHttpBindingConfig"
contract="SService.IService" />
<endpoint
address=""
binding="netTcpBinding"
bindingConfiguration="NetTCPBindingConfig"
contract="SService.IService" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:5640/SService.svc" />
<add baseAddress="net.tcp://localhost:5641/SService.svc" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
但是当我尝试向我的主机应用程序添加服务引用时,它表示无法从该地址下载元数据。我不明白什么是错的,老师从来没有教过这个......我决定继续前进,提前学习并提前学习。我使用编辑器创建了上面显示的WebConfig。
有人能指出我正确的方向吗?我通过编辑器添加了元数据行为,并将HttpGetEnabled设置为true。
答案 0 :(得分:1)
我可以在您的代码中找到一些可能导致此问题的问题:
Host = new ServiceHost(typeof(SServiceClient), BaseAddress)
。在此处typeof(SService.SService)
代替typeof(SServiceClient)
。改变如下:
Host = new ServiceHost(typeof(SService.SService))
<service behaviorConfiguration="WsHttpBehaviour"
。我猜这应该是“行为”,因为你已经定义了。由于您在配置中启用了元数据,因此您可以删除将ServiceMetadataBehavior添加到servicehost的代码行。
以下是可供参考的示例:
<system.serviceModel>
<services>
<service name="ConsoleApplication1.Service">
<endpoint address="" binding="wsHttpBinding" contract="ConsoleApplication1.IService" />
<endpoint address="" binding="netTcpBinding" contract="ConsoleApplication1.IService" />
<host>
<baseAddresses>
<add baseAddress="http://<machinename>:5640/SService.svc" />
<add baseAddress="net.tcp://<machinename>:5641/SService.svc" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ServiceHost host = new ServiceHost(typeof(Service));
host.Open();
Console.ReadLine();
}
}
[ServiceContract]
public interface IService
{
[OperationContract]
string DoWork();
}
public class Service : IService
{
public string DoWork()
{
return "Hello world";
}
}
}
元数据将在配置中定义的http baseaddress自动提供。