从ASP.NET调用WCF服务器

问题描述:

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

I have a simple WCF server that is using DuplexChannelFactory and named pipes. I can call it from a console application and I can call it from a WPF application. However, is it possible to call the same WCF server from ASP.NET and still be able to call it from console\WPF app?

我使用以下的端点:

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

我可以更新此可以被调用从ASP.NET?

Can I update this to be callable from ASP.NET?

基本上创建Ajax Enabled(启用)端点WCF服务有打电话需要从你的JavaScript方法来执行能力以下内容:

Basically to create Ajax Enabled endpoint for your wcf service to have ability to call method from javascript you need to perform the following:

1)添加到AspNetCompatibilityRequirements WCF服务的定义,所以它看起来像在以下code:

1) Adds AspNetCompatibilityRequirements to your WCF service definition, so it will look like in the following code:

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的用于售后服务将它注册生成的客户端代理

Note: namespace is also vital, because it will be used by ScriptManager to generated client side proxy after service will be registered in it.

2)然后添加[YourServiceName] .SVC与以下定义Asp.Net Web应用程序项目文件:

2) Then adds [YourServiceName].svc file with the following definition to Asp.Net web application project:

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

此文件足以注册WCF服务为阿贾克斯之一。

This file enough to register your WCF service as Ajax one.

3)然后通过添加以下到您想要使用的服务页面(或母版页)注册脚本管理该服务:

3) Then register this service with Script Manager by adding the following to pages (or Master page) where you want to use your service:

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

然后,你就可以在下面的例子一样从JavaScript调用你的服务:

Then you will be able to call your service from JavaScript like in the following example:

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

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

Some more information for example can be found in this article: http://dotnetslackers.com/articles/ajax/JSON-EnabledWCFServicesInASPNET35.aspx

修改
有几种方法脚本引用添加到脚本编程经理。第一个是ScriptManager控件本身可以用来太注册WCF服务为脚本的服务。但要获得脚本管理的当前实例,您将需要参考当前的页面实例。因此,下面code显示了如何从code来完成后面的类中的任何页面或服务器控件的:

EDIT: There are several ways to add script references to script manager programmatically. The first one is ScriptManager control itself can be used too to register wcf service as script service. But to get current instance of script manager you will need reference to current Page Instance. So, the following code shows how this can be done from code behind class of any page or server control:

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

这是例子如何从code编程方式添加的ScriptManagerProxy隐藏类的任何页面或服务器控制。这种方法要求你必须访问页面或服务器控件的控件集合:

And this is example of how to add ScriptManagerProxy programmatically from code behind class of any page or server control. This approach requires that you have access to controls collection of the page or server control:

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