从浏览器使用 WCF 库函数

Consume WCF library functions from browser

我有一个 WCF 库,其中包含我从 Powershell 和 C# 客户端使用的一些函数。现在我想直接从浏览器使用几个 util 函数,但我不知道该怎么做。

首先我在Web.config文件中添加了一个webHttpBinding端点,这里是一个切片

<services>
    <service name="MI_lib.MainService">
        <endpoint name="basic" address="" binding="basicHttpBinding" bindingConfiguration="MI_lib_http" contract="MI_lib.InterfaceMainService"></endpoint>
        <endpoint name="web" address="web" binding="webHttpBinding" bindingConfiguration="MI_lib_web" contract="MI_lib.InterfaceMainService"></endpoint>
    </service>
</services>
<bindings>
    <basicHttpBinding>
        <binding name="MI_lib_http" />
    </basicHttpBinding>
    <webHttpBinding>
        <binding name="MI_lib_web" crossDomainScriptAccessEnabled="true">
            <security mode="None"></security>
        </binding>
    </webHttpBinding>
</bindings>

为测试目的定义的一个简单函数是这个

[OperationContract]
[WebGet]
string GetData(int value);

然后,如果我连接到 http://localhost/MI_lib/MI_lib.MainService.svc/web,我会收到以下错误消息

<Fault xmlns="http://schemas.microsoft.com/ws/2005/05/envelope/none">
    <Code>
        <Value>Sender</Value>
        <Subcode>
            <Value xmlns:a="http://schemas.microsoft.com/ws/2005/05/addressing/none">a:ActionNotSupported</Value>
        </Subcode>
    </Code>
    <Reason>
        <Text xml:lang="de-CH">The message with Action '' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver. Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None).</Text>
    </Reason>
</Fault>

关于要检查什么或要提供哪些其他信息的任何提示?

要定义 Web 端点,除了使用 webHttpBinding 之外,您还需要向端点添加 Web http 行为,如下所示:

<services>
    <service name="MI_lib.MainService">
        <endpoint name="basic" address="" binding="basicHttpBinding" bindingConfiguration="MI_lib_http" contract="MI_lib.InterfaceMainService"></endpoint>
        <endpoint name="web"
                  address="web"
                  binding="webHttpBinding"
                  bindingConfiguration="MI_lib_web" contract="MI_lib.InterfaceMainService"
                  behaviorConfiguration="MyWeb">
        </endpoint>
    </service>
</services>
<bindings>
    <basicHttpBinding>
        <binding name="MI_lib_http" />
    </basicHttpBinding>
    <webHttpBinding>
        <binding name="MI_lib_web" crossDomainScriptAccessEnabled="true">
            <security mode="None"></security>
        </binding>
    </webHttpBinding>
</bindings>
<behaviors>
    <endpointBehaviors>
        <behavior name="MyWeb">
            <webHttp/>
        </behavior>
    </endpointBehaviors>
</behaviors>

此外,您需要使用的 URL 还包括方法名称,因此您需要连接到

http://localhost/MI_lib/MI_lib.MainService.svc/web/GetData?value=123