How to call a service operation at a REST style WCF endpoint uri?

杀马特。学长 韩版系。学妹 提交于 2019-12-13 07:11:21

问题


is it possible to call a service operation at a wcf endpoint uri with a self hosted service?

I want to call some default service operation when the client enters the endpoint uri of the service.

In the following sample these uris correctly call the declared operations (SayHello, SayHi):

- http://localhost:4711/clerk/hello
- http://localhost:4711/clerk/hi

But the uri

- http://localhost:4711/clerk

does not call the declared SayWelcome operation. Instead it leads to the well known 'Metadata publishing disabled' page. Enabling mex does not help, in this case the mex page is shown at the endpoint uri.

private void StartSampleServiceHost()
{
    ServiceHost serviceHost = new ServiceHost(typeof(Clerk), new Uri( "http://localhost:4711/clerk/"));
    ServiceEndpoint endpoint = serviceHost.AddServiceEndpoint(typeof(IClerk), new WebHttpBinding(), "");
    endpoint.Behaviors.Add(new WebHttpBehavior());
    serviceHost.Open();
}

[ServiceContract]
public interface IClerk
{
    [OperationContract, WebGet(UriTemplate = "")]
    Stream SayWelcome();

    [OperationContract, WebGet(UriTemplate = "/hello/")]
    Stream SayHello();

    [OperationContract, WebGet(UriTemplate = "/hi/")]
    Stream SayHi();
}    

public class Clerk : IClerk
{
    public Stream SayWelcome() { return Say("welcome"); }

    public Stream SayHello() { return Say("hello"); }

    public Stream SayHi() { return Say("hi"); }

    private Stream Say(string what)
    {
        string page = @"<html><body>" + what + "</body></html>";
        return new MemoryStream(Encoding.UTF8.GetBytes(page));
    }
}

Is there any way to disable the mex handling and to enable a declared operation instead?

Thanks in advance, Dieter


回答1:


Did you try?

[OperationContract, WebGet(UriTemplate = "/")]
Stream SayWelcome();

UPDATE:

Not sure why it is not working for you, I have a self hosted WCF service with the following service contract:

[ServiceContract]
public interface IDiscoveryService {

    [OperationContract]
    [WebGet(BodyStyle=WebMessageBodyStyle.Bare, UriTemplate="")]
    Stream GetDatasets();

The only difference I can see is that I use WebServiceHost instead of ServiceHost.



来源:https://stackoverflow.com/questions/2903652/how-to-call-a-service-operation-at-a-rest-style-wcf-endpoint-uri

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!