2012-10-14 48 views
2

我有一個使用DuplexChannelFactory和命名管道的簡單WCF服務器。我可以從控制檯應用程序調用它,並且可以從WPF應用程序調用它。但是,是否可以從ASP.NET調用相同的WCF服務器,並且仍然可以從控制檯\ WPF應用程序調用它?從ASP.NET調用WCF服務器

我使用以下端點:

[ServiceContract(SessionMode = SessionMode.Required, 
    CallbackContract = typeof(IMyWCFClient))] 
public interface IMyWCFServer 
{ 
    [OperationContract] 
    bool Subscribe(); 
    [OperationContract] 
    bool UnSubscribe(); 
} 

我可以更新此可以被調用從ASP.NET?

+3

您是否嘗試在ASP.NET應用程序中調用**當前服務**?我認爲你只需要在客戶端(在這種情況下是Web應用)配置服務,就像在控制檯和WPF應用上配置服務一樣。另一方面,如果您想通過HTTP公開相同的服務,那麼您將需要在主機Web應用程序中配置新的端點。 – Jupaol

回答

2

基本上創建Ajax啓用端點的WCF服務有打電話需要從你的JavaScript方法執行以下能力:

1)添加AspNetCompatibilityRequirements到WCF服務的定義,所以它看起來像下面的代碼:

namespace Test 

[ServiceContract(Namespace = "Test.Services")] 
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
public class TestService : IMyWCFServer 
{ 
    // To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json) 
    // To create an operation that returns XML, 
    //  add [WebGet(ResponseFormat=WebMessageFormat.Xml)], 
    //  and include the following line in the operation body: 
    //   WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml"; 

    public bool UnSubscribe() 
    { 
     return true; 
    } 

    public bool Subscribe() 
    { 
     return false; 
    } 
} 

注:命名空間也是必不可少的,因爲它會通過ScriptManager的用來生成客戶端代理服務後,將被註冊它。

2)然後添加[YourServiceName] .SVC與以下定義Asp.Net Web應用程序項目文件:

<%@ ServiceHost 
Language="C#" Debug="true" 
Service="Test.TestService " 
Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" %> 

該文件足以註冊WCF服務作爲阿賈克斯之一。

3)然後通過添加以下到您想要使用的服務頁面(或母版頁)註冊腳本管理這個服務:

<asp:ScriptManagerProxy runat="server" ID="ScriptManagerProxy"> 
    <Services> 
     <asp:ServiceReference Path="~/[RelativePathToSVCFile].svc" /> 
    </Services> 
</asp:ScriptManagerProxy> 

然後,你就可以從JavaScript調用服務就像下面這個例子:

var wasSubscribed = Test.Services.TestService.Subscribe(); 

例如一些更多的信息,可以在本文中找到:http://dotnetslackers.com/articles/ajax/JSON-EnabledWCFServicesInASPNET35.aspx

編輯: 有幾種方法可以通過編程方式向腳本管理器添加腳本引用。第一個是ScriptManager控件本身也可用於將wcf服務註冊爲腳本服務。但要獲得當前的腳本管理器實例,您需要參考當前的頁面實例。所以,下面的代碼顯示瞭如何從代碼來完成後面的類中的任何頁面或服務器控件的:

protected void Page_Load(object sender, EventArgs e) 
    { 
     ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page); 
     scriptManager.Services.Add(new ServiceReference { Path = "[RelativePathToSVCFile].svc" }); 
    } 

這是例子如何從代碼編程方式添加的ScriptManagerProxy背後類中的任何頁面或服務器控件。此方法要求您有權訪問控件頁面或服務器控件的集合:

/// <summary> 
    /// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering. 
    /// </summary> 
    protected override void CreateChildControls() 
    { 
     base.CreateChildControls(); 

     ScriptManagerProxy scriptManagerProxy = new ScriptManagerProxy { ID = "ScriptManagerProxy" }; 
     this.Controls.Add(scriptManagerProxy); 

     scriptManagerProxy.Services.Add(new ServiceReference { Path = "[RelativePathToSVCFile].svc" }); 
    } 
+0

我也忘記提到wcf服務的實現甚至可以位於單獨的Assembly中(在這種情況下,您只需要添加對它的引用)。因此,您將能夠使用現有的綁定到您的wcf服務+將會使用[YourServiceName] .svc文件指定的Ajax綁定。 –

+0

你知道如何添加以編程方式添加ScriptManagerProxy,因爲我不需要一個控件來容納ScriptManagerProxy? –

+0

我更新了我的帖子。請參閱編輯部分。 –

5

是的,你可以通過控制檯,WPF和ASP.NET調用WCF服務。但是您首先需要創建適當的endpoint

How to: Create a Service Endpoint in Configuration

+0

我已使用我的端點更新了問題 - 如何更新以支持ASP.NET? –

+0

對不起,親愛的你只給了契約而不是終點。通常端點在配置文件中。 – jams