2015-04-29 22 views
0

我有一個WCF庫,其中包含一些我從Powershell和C#客戶端使用的函數。現在我想直接從瀏覽器使用幾個util函數,但我不知道該怎麼做。從瀏覽器中消耗WCF庫函數

首先,我在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> 

提示要檢查什麼或要提供哪些其他信息?

+0

你會爲一個REST服務,因爲你使用的WebHttpBinding,你需要定義aditional的信息給webget attirbute像模板URI所以你可以傳遞你的參數並使用getdata而不是web調用方法 – Coder1409

回答

1

要定義一個網絡終端,你所需要的,除了使用webHttpBinding,到網絡的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