开发者

ServiceReference a self hosted WCF service

I am currently maintaining and developing a website which uses a lot of webservices in an ajax way.

Registering of the services is done in the aspx like this:

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

and consuming the services in the javascript is done like this

WSAdministrator.GetConsumerClubInfo(ConsumerClubId,
                                    OnSucceededToGetConsumerClubInfo,
                                    OnFailedToGetConsumerClubInfo);

I want to know if I can reference a self-hosted WCF service(on the same machine) this easily.

any suggestions?

EDIT: The WCF service is running on a windows service, it exposes both webHttpBinding and basicHttpBinding endpoints.

After Reading ASP.Net WCF Service with no App_Code , I realized that I should just create an svc file which will act as a reference to the service.

I created this svc file:

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

and in the web.config file I added these lines:

        <services>
        <service name="MyService.Namespace.Contract">
          开发者_如何转开发  <endpoint address="setAddress" binding="basicHttpBinding" contract="MyService.Namespace.ContractInterface"/>
        </service>
    </services>

The address is working, but when I try to access the reference from the svc, I get the following error:

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

What am I missing here?

Note: There have been some nice answers, but all to things I already know, my question is about how to reference my Self Hosted WCF service using asp.net so that I can use it from javascript, that's all, and for that I still have no answers...

I saw some replies to similar questions telling there should be an IIS hosted service acting as a "pipe" to the actual service, and then the ScriptManager should reference it, Maybe that's the only answer...


When you are self hosting your WCF Service you do not use .SVC file, but create the service host in your windows service's OnStart method in the following way.

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

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

If you want to host your service to support WebHttpBinding then the hosting class should be WebServiceHost and if you want to host wsHttpBinding or any other you should use ServiceHost.

Once service starts running clients can connect to it.

The following link contains step by step process for doing it.

If you have to support RESTful service that is able to talk to using AJAX and Jquery then you should go with WebServiceHost and you would decorate your operation contracts in the following way.

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

You can find some info on this even in the following question.


Of course you can and it would look like this with WCF,

<asp:ServiceReference Path="~/WSAdministrator.svc" />

See Here and here for some examples.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜