加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – ServiceReference是一个自托管的WCF服务

发布时间:2020-12-16 09:23:01 所属栏目:asp.Net 来源:网络整理
导读:我目前正在维护和开发一个以ajax方式使用大量web服务的网站. 注册服务是在aspx中完成的,如下所示: asp:ScriptManagerProxy id="ScriptManager1" runat="server" services asp:ServiceReference Path="WebServices/WSAdministrator.asmx"/asp:ServiceReferen
我目前正在维护和开发一个以ajax方式使用大量web服务的网站.

注册服务是在aspx中完成的,如下所示:

<asp:ScriptManagerProxy id="ScriptManager1" runat="server">
        <services>    
            <asp:ServiceReference Path="WebServices/WSAdministrator.asmx"></asp:ServiceReference>
        </services>
</asp:ScriptManagerProxy>

并在javascript中使用服务是这样完成的

WSAdministrator.GetConsumerClubInfo(ConsumerClubId,OnSucceededToGetConsumerClubInfo,OnFailedToGetConsumerClubInfo);

我想知道我是否可以轻松地引用自托管WCF服务(在同一台机器上).

有什么建议?

编辑:WCF服务在Windows服务上运行,它公开webHttpBinding和basicHttpBinding端点.

阅读ASP.Net WCF Service with no App_Code之后,我意识到我应该创建一个svc文件,它将作为服务的引用.

我创建了这个svc文件:

<%@ ServiceHost Language="C#" Service="MyService.Namespace.Contract" %>

在web.config文件中我添加了以下行:

<services>
        <service name="MyService.Namespace.Contract">
            <endpoint address="setAddress" binding="basicHttpBinding" contract="MyService.Namespace.ContractInterface"/>
        </service>
    </services>

地址正常,但是当我尝试从svc访问引用时,我收到以下错误:

The type ”,provided as the Service attribute value in the
ServiceHost directive could not be found.

我在这里错过了什么?

注意:有一些很好的答案,但所有我已经知道的事情,我的问题是如何使用asp.net引用我的自托管WCF服务,以便我可以从javascript使用它,这就是全部,为此我仍然没有答案……

我看到一些类似问题的回复告诉我们应该有一个IIS托管服务作为实际服务的“管道”,然后ScriptManager应该引用它,也许这是唯一的答案……

解决方法

当您自托管WCF服务时,不要使用.SVC文件,而是按照以下方式在Windows服务的OnStart方法中创建服务主机.

WebServiceHost myServiceHost = null;
if (myServiceHost != null)
{
    myServiceHost.Close();
}

myServiceHost = new WebServiceHost(typeof(YourClassName));
myServiceHost.Open();

如果您想托管您的服务以支持WebHttpBinding,那么托管类应该是WebServiceHost,如果您想托管wsHttpBinding或其他任何您应该使用ServiceHost.

一旦服务开始运行,客户端就可以连接到它.

以下link包含了执行此操作的分步过程.

如果你必须支持能够使用AJAX和Jquery进行交谈的RESTful服务,那么你应该使用WebServiceHost,你将以下列方式装饰你的操作合同.

[ServiceContract()]
public interface IMyInterface
{
   [OperationContract]
   [WebInvoke(Method = "GET",ResponseFormat = WebMessageFormat.Json,UriTemplate = "GetArray",BodyStyle = WebMessageBodyStyle.Bare)]
   MyArray[] GetArray();
}

您甚至可以在以下question中找到有关此信息.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读