WCF服务自托管url没有获取wsdl文件

时间:2014-06-03 06:30:10

标签: c# .net wcf self-hosting

namespace helloserviceSelfHostingDemo
{
    [ServiceContract]
    interface IhelloService
    {
        [OperationContract]
        string sayhello(string name);
    }

public class HelloService : IhelloService
{

    public string sayhello(string name)
    {
        return "hello " + name;
    }
}
class Program
{
    static void Main(string[] args)
    {
        ServiceHost host = new ServiceHost(typeof(HelloService));
        BasicHttpBinding bind = new BasicHttpBinding();
        host.AddServiceEndpoint(typeof(IhelloService), bind, "http://8080/myhelloservice");
        host.Open();
        Console.WriteLine("hello service is running");
        Console.ReadKey();
    }
}

}

这段代码运行良好但是当我复制时,浏览器中的这个地址没有获得服务

3 个答案:

答案 0 :(得分:3)

你需要你的mex绑定,如下所示:

string mexAddress = "http://localhost:8000/servicemodelsamples/service/mex";
MetadataExchangeClient mexClient = new MetadataExchangeClient("MyMexEndpoint");
mexClient.ResolveMetadataReferences = true;
MetadataSet mdSet = mexClient.GetMetadata(new EndpointAddress(mexAddress));

如果没有Mex,当导航到URL时,没有要发布的元数据。

答案 1 :(得分:2)

您需要公开元数据信息。

Uri baseAddress = new Uri("http://localhost:8080/hello");

using (ServiceHost host = new ServiceHost(typeof(HelloWorldService), baseAddress))
{
    ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
    smb.HttpGetEnabled = true;
    smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
    host.Description.Behaviors.Add(smb);
    host.Open();

    Console.WriteLine("The service is ready at {0}", baseAddress);
    Console.WriteLine("Press <Enter> to stop the service.");
    Console.ReadLine();
    host.Close();
}

来源:http://msdn.microsoft.com/en-us/library/ms731758(v=vs.110).aspx

答案 2 :(得分:0)

在WCF服务web.config文件中添加以下端点

<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />