简体   繁体   中英

Can you use WCF services with Windows Phone 7?

I've only been able to find a couple of people online mentioning this, and it seems to be as simple for them as adding the service reference.

However, when I attempt to add a service reference for my WCF service (which works correctly in a regular console app, so I have ruled the WCF out as the problem) I receive a host of errors.

  • Warning 5 Custom tool warning: No endpoints compatible with Silverlight 3 were found. The generated client class will not be usable unless endpoint information is provided via the constructor.
  • Warning 6 Custom tool warning: Exception has been thrown by the target of an invocation.
  • Warning 2 Custom tool warning: Cannot import wsdl:portType Detail: An exception was thrown while running a WSDL import extension: System.ServiceModel.Description.DataContractSerializerMessageContractImporter
  • Error: Could not load type 'System.Runtime.Serialization.DataContractSet' from assembly 'System.Runtime.Serialization, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.
  • Warning 4 Custom tool warning: Cannot import wsdl:port Detail: There was an error importing a wsdl:binding that the wsdl:port is dependent on.
  • Warning 3 Custom tool warning: Cannot import wsdl:binding Detail: There was an error importing a wsdl:portType that the wsdl:binding is dependent on.
  • Error 7 Custom tool error: Failed to generate code for the service reference 'ServiceReference'. Please check other error and warning messages for details.

I find it strange that the first error mentions Silverlight 3, as I just completely uninstalled and reinstalled all of my development tools to ensure that I was using the latest version of .NET and Silverlight.

These errors look similar to the ones I see if I attempt to create a new Silverlight project and do not check the box "Enable WCF RIA services". I have not been able to find any means to enable that for WP7, if that is indeed the problem.

Any assistance from you scholarly gentlemen (and gentlewomen) would be much appreciated.

Mmmm, I just changed the service reference config by unticking the "reuse type ...." box and then update worked and it generated the service config file. Not sure if this will help you?

Remember that Silverlight - even for Windows Phone, which uses a specialized version of Silverlight 3 - does NOT work with all WCF Service bindings/endpoints, but instead only supports a subset (which excludes WS-HTTP bindings, for example.) Your simplest bet is to create your WCF service for Silverlight applications using the "Silverlight-enabled WCF Service" template in Visual Studio (under Add/New Item/Silverlight).

This blog entry ( http://blogs.msdn.com/b/silverlightws/archive/2009/03/20/what-s-new-with-web-services-in-silverlight-3-beta.aspx ) from the Silverlight WCF Team Blog should shed some light on the possibilities. You may want to check out he whole blog ( http://blogs.msdn.com/b/silverlightws/ ), as it has some interesting how-to and gotcha articles.

I ran into the exact same error, and changed the collection type to System.Collections.Generic.List instead of System.Collections.ObjectModel.ObservableCollections. This was odd since I did not even check the box for "Always generate message contracts".

Hope that helps.

This problem occurred when you run the Visual Studio with Administrator Privilege. What you can do for a workaround is run the Visual Studio without Administrator Privilege, add the web service reference that will generate the proxy classes and close the solution. Open the project solution again in Visual Studio with Administrator privilege. A bug report already file in Microsoft Connect.

https://connect.microsoft.com/VisualStudio/feedback/details/624984/error-warnings-when-adding-web-reference-on-windows-phone-7-project?wa=wsignin1.0

Yes this is one strange Error. You will be able to return Strings, Integers etc but anything else like ArrayList and so forth you will get this error.

There is nothing wrong with your code, there is just a bug with VS. Microsoft have fixed it (check service packs) but if you are programming for Windows Phone 7, the bug is still there.

You have Delete the Service, Then Bin and Obj Folder. Save and close VS. Then restart the Project, Add the Service Ref (DO NOT DEBUG/RUN the app). Oh Yes DO A BACKUP FIRST. It should be ok. You may have to repeat this process everytime you Update the Service.

This should fix it, if not, you may have to open a new project or if possible recreate the Webservice.

The answer is yes, you can access WCF services from WP7, and like a previous poster noted it only supports WS-HTTP. In the case that you are trying to access a WCF Service for a Silverlight application then there are a few things that you need to do:

  1. Update your Silvleright Toolkit to the latest. Go get it from Codeplex
  2. Add a reference to Microsoft.ServiceModel.DomainServices.Hosting to your silverlight project.
  3. Go to the Web.Config and add the soap endpoint:

     \n<domainServices> \n    <endpoints> \n        <add name="Soap" \n             type="Microsoft.ServiceModel.DomainServices.Hosting.SoapXmlEndpointFactory, \nMicrosoft.ServiceModel.DomainServices.Hosting" \n        /> \n    </endpoints> \n</domainServices> 

    Do not worry about the "system.serviceModel" warning; ignore it

  4. Add the service reference. "But, which uri?" you ask. The address of your service is this one: [namespace of your ria service]-[classname of your ria service].svc where . are replaced with - . So, if I have created my service inside of a Services directory in my SL application and the namespace looks like this:

    \nnamespace myApplication.Web.Services\n{ \n    [EnableClientAccess()] \n    public class SuperService ....\n

then the address would be:

http://localhost[:port]/Services/myApplication-Web-Services-SuperService.svc

Let the tooling do the rest. If you get some funky errors then save and close VS and start again and it all works.

I hope that this is the answer that you are looking for.

The following solved my problem:

I created a new WP7 project added the wcf service.

Then I copied the Service References folder to the directory of my project that gave me this problem and restarted Visual studio and built the application.

You should get namespace errors inside your Reference.cs ; just change the namespace to the current project namespace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM