Not able to add Custom Rest Data Service as Service Reference in VS2010

已答覆 Not able to add Custom Rest Data Service as Service Reference in VS2010

  • 2012年7月30日 上午 09:55
     
      包含代碼

    Hi,

    I have created a WCF DataService and had deployed to the SharePoint 2010 successfully. I'm able to access the svc file successfully through the browser (e.g. http:/<url>/_vti_bin/revert/Revert.svc</url>, http:/<url>/_vti_bin/revert/Revert.svc/Revert/a/aa</url>).

    When I try to add the url as a service reference, I'm not able to add as it give an error as below.

    Below is the code for my Data Service

    Revert.svc

    <%@ServiceHost Language="C#" Debug="true"
        Service="WcfService.RevertService, $SharePoint.Project.AssemblyFullName$"
        Factory="Microsoft.SharePoint.Client.Services.MultipleBaseAddressWebServiceHostFactory, Microsoft.SharePoint.Client.ServerRuntime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

    IRevert.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.Text;
    using System.ServiceModel.Web;
    
    namespace WcfService
    {
        [ServiceContract(Name = "RevertService", Namespace = "http://www.company.com/revertservice")]
        public interface IRevert
        {
            [OperationContract]
            [WebGet(UriTemplate = "")]
            List<string> FirstLoad();
    
            [OperationContract]
            [WebGet(UriTemplate = "/Revert/{listName}/{listItemId}")]
            List<string> Reverts(string listName, string listItemId);
        }
    }

    RevertService.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.Text;
    using Microsoft.SharePoint.Client.Services;
    using System.ServiceModel.Activation;
    using Microsoft.SharePoint;
    
    namespace WcfService
    {
        [BasicHttpBindingServiceMetadataExchangeEndpointAttribute]
        [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
            public class RevertService : IRevert
        {
            public List<string> FirstLoad()
            {
                List<string> s = new List<string>();
                s.Add("Revert()");
                s.Add("Reverts(string listName, string listItemId)");
                return s;
            }
    
            public List<string> Reverts(string listName, string listItemId)
            {
                List<string> s = new List<string>();
                s.Add("Revert Method " + listName);
                s.Add("Revert Method " + listItemId);
                return s;
            }
        }
    }

    Is there something wrong with the data service (Revert.svc)?

    I did not made any changes made to web.config in inetpub and isapi folder.

    Link to the WCF Data Service Source Code

    Thanks in advance.


    jingzo (^_^)



    • 已編輯 jingzo 2012年7月31日 上午 06:06
    • 已編輯 jingzo 2012年8月9日 下午 03:02 Incorrect title
    •  

所有回覆

  • 2012年7月30日 上午 11:06
     
     

    Hi,

    I think you need put '/mex' at the end of the url, when you add reference in VS. Please check the following url for more details:

    http://ranaictiu-technicalblog.blogspot.com/2011/03/sharepoint-2010-create-custom-wcf.html


    Thanks,
    Sohel Rana
    http://ranaictiu-technicalblog.blogspot.com

  • 2012年7月30日 下午 12:48
     
     

    Thank you for your reply.

    I'm not able to access this URL (http://<url>/_vti_bin/revert/Revert.svc/mex) via browser also. The WCF data service type is REST Service.

    Thanks.

    To addon, the url (<a href="http:///_vti_bin/revert/Revert.svc/mex">http://<url>/_vti_bin/revert/Revert.svc/mex) give "Endpoint not found." message.


    jingzo (^_^)



    • 已編輯 jingzo 2012年7月30日 下午 12:49
    • 已編輯 jingzo 2012年7月31日 上午 06:07
    •  
  • 2012年8月6日 上午 08:27
    版主
     
     已答覆
    Hi jingzo,

    The clue is in the error - it's saying the binding is expecting windows authentication with an NTLM token. VS will be making an anonymous request to the mex endpoint (which should allow anonymous access as it exposes the meta data).
    Although the service should use dynamic endpoints - you could try explicitly defining the bindings and endpoints in the web.config so you know what's going on.
    * It may be the case that IIS is not allowing anonymous access to the service, so check that anonymous access is enabled on the virtual directory for the service (I've not got access to my SP machine now so can't point you at the right place).

    Thanks,
    Lhan Han
  • 2012年8月9日 下午 03:23
     
     

    Thank you for your replies.

    I'm able to Add http://url/_vti_bin/ListData.svc as a service reference in my client application but not my custom WCF service. Both ListData.svc and my custom WCF service Factory is using MultipleBaseAddressWebServiceHostFactory.

    By using MultipleBaseAddressWebServiceHostFactory, the endpoint will be created by default.

    Using MultipleBaseAddressWebServiceHostFactory as factory, somehow /MEX will not be loaded correctly, giving EndPoint not found.

    I still not able to see what's wrong with my custom WCF service. Anyone can help?


    jingzo (^_^)

  • 2012年8月9日 下午 03:53
     
     

    Unlike compiled refereneces in ASMX, Microsoft designed WCF to be configured and connected through web.config files.  This was done to minimize the need to recompile services each time connection specs have changed.

    Unfortunately, the binding settings are different between the .NET 3.5 and 4.0 frameworks.  Here is a web.config file with the settings for both platforms.  Comment out the parts of the one that isn’t being used.  Build the WCF service under the appropriate framework.

    <?xml version="1.0"?>

    <configuration>

      <configSections>

        <!--section groups for .NET4 START -->

        <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">

          <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">

            <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>

            <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">

              <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>

              <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>

              <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>

              <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>

            </sectionGroup>

          </sectionGroup>

        </sectionGroup>

        <!--section groups for .NET4 END -->

      </configSections>

      <system.web>

        <compilation debug="true">

          <assemblies>

            <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>

            <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

            <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>

            <add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>

          </assemblies>

        </compilation>

        <pages>

          <controls>

            <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

            <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

          </controls>

        </pages>

        <httpHandlers>

          <remove verb="*" path="*.asmx"/>

          <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

          <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

          <add verb="GET,HEAD" path="ScriptResource.axd" validate="false" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

        </httpHandlers>

        <httpModules>

          <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

        </httpModules>

      </system.web>

      <!--system.servicemodel for .NET35 START -->

      <system.serviceModel>

        <services>

          <service name="InfotekSPS2010.InfotekSPS2010" behaviorConfiguration="returnFaults">

            <endpoint contract="InfotekSPS2010.IInfotekSPS2010" binding="basicHttpBinding" address=""/>

          </service>

        </services>

        <bindings>

          <basicHttpBinding>

            <binding name="HttpBinding" maxReceivedMessageSize="2097152"/>

          </basicHttpBinding>

        </bindings>

        <behaviors>

          <serviceBehaviors>

            <behavior name="returnFaults">

              <serviceDebug includeExceptionDetailInFaults="true"/>

              <serviceMetadata httpGetEnabled="true"/>

            </behavior>

          </serviceBehaviors>

        </behaviors>

      </system.serviceModel>

      <!--system.servicemodel for .NET35 END -->

      <!--system.servicemodel for .NET4 START -->

      <system.serviceModel>

        <behaviors>

          <serviceBehaviors>

            <behavior>

              <serviceMetadata httpGetEnabled="true"/>

              <serviceDebug includeExceptionDetailInFaults="true"/>

            </behavior>

          </serviceBehaviors>

        </behaviors>

        <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>

      </system.serviceModel>

      <!--system.servicemodel for .NET4 END -->

      <system.webServer>

        <modules runAllManagedModulesForAllRequests="true">

          <remove name="ScriptModule"/>

          <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

        </modules>

        <directoryBrowse enabled="true"/>

        <validation validateIntegratedModeConfiguration="false"/>

        <handlers>

          <remove name="WebServiceHandlerFactory-Integrated"/>

          <remove name="ScriptHandlerFactory"/>

          <remove name="ScriptHandlerFactoryAppServices"/>

          <remove name="ScriptResource"/>

          <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

          <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

          <add name="ScriptResource" verb="GET,HEAD" path="ScriptResource.axd" preCondition="integratedMode" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

        </handlers>

      </system.webServer>

      <system.codedom>

        <compilers>

          <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="4">

            <providerOption name="CompilerVersion" value="v3.5"/>

            <providerOption name="WarnAsError" value="false"/>

          </compiler>

        </compilers>

      </system.codedom>

      <runtime>

        <assemblyBinding appliesTo="v2.0.50727" xmlns="urn:schemas-microsoft-com:asm.v1">

          <dependentAssembly>

            <assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/>

            <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>

          </dependentAssembly>

          <dependentAssembly>

            <assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/>

            <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>

          </dependentAssembly>

        </assemblyBinding>

      </runtime>

    </configuration>