开发者

WCF service accessed from silverlight

开发者 https://www.devze.com 2023-03-04 20:22 出处:网络
My application consist of an MVC3 website that contains a silverlight control. In the MVC3 application I host a WCF service. I use that service in the silverlight control to get data from the DB.

My application consist of an MVC3 website that contains a silverlight control. In the MVC3 application I host a WCF service. I use that service in the silverlight control to get data from the DB.

In the MVC web.config the service is defined as:

<system.serviceModel>
<behaviors>
  <endpointBehaviors>
    <behavior name="OnlineCustomersTracker.Services.CustomersTrackerServiceAspNetAjaxBehavior">
      <enableWebScript />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
  multipleSiteBindingsEnabled="true" />
<services>
  <service name="OnlineCustomersTracker.Services.CustomersTrackerService">
    <endpoint address="" behaviorConfiguration="OnlineCustomersTracker.Services.CustomersTrackerServiceA开发者_C百科spNetAjaxBehavior"
      binding="webHttpBinding" contract="OnlineCustomersTracker.Services.CustomersTrackerService" />
  </service>
</services>

The method that I call is:

 [OperationContract]
 [WebGet(ResponseFormat = WebMessageFormat.Xml)]
 public string DoWork()
 {
    // Add your operation implementation here
    return "Work done";
 }

In Silverlight I call the service:

CustomersTrackerServiceClient client = new CustomersTrackerServiceClient(new BasicHttpBinding(),
                                                                                 new EndpointAddress(
                                                                                     "http://localhost:62535/Services/CustomersTrackerService.svc"));
        client.DoWorkCompleted += new System.EventHandler<DoWorkCompletedEventArgs>(client_DoWorkCompleted);
        client.DoWorkAsync();

When I access the service from silverlight I get the exception: The remote server returned an error: NotFound.

The strange part is that the service is working from the web browser. Am I doing something wrong in silverlight?

WCF service accessed from silverlight

I traced the call from firefox and the silverlight control is doing a post to the service address: http://localhost:62535/Services/CustomersTrackerService.svc. It seems that my service is listening only for get requests. Is there any way to allow post request ... or to configure the service in silverlight to make get requests?


Maybe you haven't configured yet the cross-domain access policy...

Check this out, it could be what you missed:

http://www.codeproject.com/KB/silverlight/4StepsSilverLight.aspx

;-)

Richie


This general error is not really very helpful. In general you might have some more luck by enabling logging in your service as this usually gives some more pointers as to what is going wrong, as this error gets thrown in many situations.

To enable logging you can add this to the config file for the service:

<configuration>
....
<system.diagnostics>
<trace autoflush="true" />
<sources>
  <source name="System.ServiceModel" switchValue="Verbose">
    <listeners>
      <add name="sdt" type="System.Diagnostics.XmlWriterTraceListener" initializeData="D:\path\to\wcfLog.svcLog" />
    </listeners>
  </source>
</sources>
</system.diagnostics>
</configuration>

The other problem you are likely to have is the cross domain issue, especially when you first try and access the site for the first time with a silverlight client. You should add a crossdomain.xml and a clientaccesspolicy.xml at the root of the web site.

These should contains xml like this:

CrossDomain.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
   <allow-http-request-headers-from domain="*" headers="*" />
 </cross-domain-policy>

ClientAccessPolicy.xml

<?xml version="1.0" encoding="utf-8"?>
<access-policy>
  <cross-domain-access>
    <policy>
      <allow-from http-request-headers="*">
        <domain uri="*"/>
      </allow-from>
      <grant-to>
        <resource include-subpaths="true" path="/"/>
      </grant-to>
    </policy>
  </cross-domain-access>
</access-policy>

Silverlight clients will try and access these files at the root of your site and so you should serve them from there, which should be straightforward if it is an ASP site hosted in IIS. Details on MSDN.

You can provide a WCF service to serve them if your service is self hosted but it is more complicated to set up. Some details on a self hosted solution


I finally found the problem. I did not configure correctly the endpoints. Actually I had only one endpoint the json one. I found a great article for setting up a service with multiple endpoints. http://www.codemeit.com/wcf/wcf-restful-pox-json-and-soap-coexist.html

0

精彩评论

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