如何在REST样式WCF端点uri上调用服务操作? [英] How to call a service operation at a REST style WCF endpoint uri?

查看:106
本文介绍了如何在REST样式WCF端点uri上调用服务操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过自托管服务在wcf端点uri上调用服务操作?

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

当客户端输入服务的端点uri时,我想调用一些默认的服务操作.

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

在以下示例中,这些uri正确调用了声明的操作(SayHello,SayHi):

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

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

但是uri

- http://localhost:4711/clerk

不调用声明的SayWelcome操作.而是指向著名的禁止元数据发布"页面.启用mex并没有帮助,在这种情况下,端点uri会显示mex页面.

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));
    }
}

有什么方法可以禁用mex处理并启用声明的操作吗?

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

预先感谢迪特

推荐答案

您尝试过吗?

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

更新:

不知道为什么它对您不起作用,我有一个带有以下服务合同的自托管WCF服务:

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();

我唯一看到的区别是我使用WebServiceHost而不是ServiceHost.

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

这篇关于如何在REST样式WCF端点uri上调用服务操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆