开发者

Calling a Winform hosted WCF service from javascript

I have a Winform hosted WCF service with Ajax Web endpoint.

I also have an ASP.NET project separately, with ScriptManager component on the page.

My question is, should that work if I make service calls to my Winform hosted service from the client side of ASP.NET app using javascript?

My ASP.NET Default page looks like this:

<script type="text/javascript">    
function Button1_onclick() {        
// ????How to call a service method?????    
}
</script>

<asp:ScriptManager ID="ScriptManager1" runat="server">
    <Services>
           <asp:ServiceReference Path="http:开发者_JS百科//localhost:8000/web" />  //My winform hosted service
    </Services>
</asp:ScriptManager>

Or do I have to host my service in IIS to be consumable by AJAX?


Regardless of whether you are self hosting or IIS hosting the WCF service, if your website and service are not at the same address, (protocol, servername, port) then you will run into XSS (Cross Site Scripting) restrictions.

The MSDN Documentation on ServiceReference.Path states:

"The Path property can point only to local Web services. In other words, this property can only point to Web services in the same domain as your AJAX-enabled ASP.NET Web application. Path can be a relative, application-relative, domain-relative, or absolute path."

The 2 best options are:

  1. Create a proxy locally - that is, replicate the interface of the service in your local web, then hand off the request to the web service from the code.
  2. Change the service to return its result in JSON format. This will allow you to add a dynamic script tag to retrive the results.

Both of these options are outlined here.


It turned out to be pretty simple with the right service configuration:

<endpoint address="Web/" binding="webHttpBinding" contract="IMyService"  
          behaviorConfiguration="WebBehavior"/>
<!-- ... -->
<endpointBehaviors>
    <behavior name="WebBehavior">
        <webHttp defaultOutgoingResponseFormat="Json" defaultBodyStyle="Wrapped" />
    </behavior>
</endpointBehaviors>

I also added a WebInvoke attribute on my service contract:

[WebInvoke(Method = "POST")]
public interface IMyService {
    // ...
}

With this configuration you can make a call to the service method just using your browser. So javascript only has to make a POST HTTP query to the url, that's a trivial task that doesn't require any ASP.NET ServiceManager stuff. jQuery sample:

<script type="text/javascript"> 
function Button1_onclick() {        
    $.ajax({
        type: "POST",
        url: "http://localhost:8000/web/",
        data: "",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        processdata: true
        success: function(msg) { /*...*/ },
        error: /*..error handler..*/
    });
}
</script>

In my case I didn't have to pass any parameters to the service (in fact, my method is marked as a one way method). But adding parameters would only complicate things a bit (you would to pass a json string in instead of an empty string as a data).


You can host your WCF service anywhere, as long as:

  • it is up and running when the client connects to it
  • you specify the port in the client code
  • your firewall doesn't block the used port (should be ok if client and server are running on the same box)

It doesn't have to be running in IIS.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜