【问题标题】:Calling WCF Server from ASP.NET从 ASP.NET 调用 WCF 服务器
【发布时间】:2012-10-14 22:02:57
【问题描述】:

我有一个使用 DuplexChannelFactory 和命名管道的简单 WCF 服务器。我可以从控制台应用程序调用它,也可以从 WPF 应用程序调用它。但是,是否可以从 ASP.NET 调用相同的 WCF 服务器并且仍然能够从 console\WPF 应用程序调用它?

我正在使用以下端点:

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

我可以将其更新为可从 ASP.NET 调用吗?

【问题讨论】:

  • 您是否尝试在 ASP.NET 应用程序中调用您的当前服务?我认为您只需要在客户端(在本例中为 Web 应用程序)配置服务,就像在控制台和 WPF 应用程序上配置它一样。另一方面,如果您想通过 HTTP 公开相同的服务,则需要在 Host Web App 中配置一个新端点

标签: asp.net wcf


【解决方案1】:

是的,您可以通过控制台、WPF 和 ASP.NET 调用 WCF 服务。但您需要先创建适当的endpoint。

How to: Create a Service Endpoint in Configuration

【讨论】:

  • 我已经用我的端点更新了 ques - 我如何更新以支持 ASP.NET?
  • 对不起,亲爱的,您只给了合同而不是端点。通常端点在配置文件中。
【解决方案2】:

基本上要为您的 wcf 服务创建启用 Ajax 的端点以能够从 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 服务注册为 Ajax 服务。

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

【讨论】:

  • 我还忘了提到 wcf 服务的实现甚至可以位于单独的程序集中(在这种情况下,您只需添加对它的引用)。因此,您将能够使用现有的绑定到您的 wcf 服务 + 将使用 [YourServiceName].svc 文件指定的 Ajax 绑定。
  • 您知道如何以编程方式添加 ScriptManagerProxy,因为我不需要控件来容纳 ScriptManagerProxy?
猜你喜欢
  • 1970-01-01
  • 2020-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多