简体   繁体   English

WCF WebService使用POST。 命名空间错误?

[英]WCF WebService using POST. Namespace error?

I have a RESTful service with the interface below, using POST. 我在下面的界面中使用POST提供了RESTful服务。 When I try to go to the service URL I get a server error saying something like there is a namespace conflict, but I don't think that is the case. 当我尝试转到服务URL时,出现服务器错误,提示存在名称空间冲突之类的信息,但我认为并非如此。 Here is a shortened version of the error message. 这是错误消息的简化版本。

System.InvalidOperationException: DataContract for type 'xyz.EntityList`1[zxc]' cannot be added to DataContractSet since type 'xyz.EntityList`1[zxc]' with the same data contract name 'EntityList' in namespace 'Bugg' is already present and the contracts are not equivalent.

The rest of the error is given below. 其余的错误在下面给出。

namespace MyComp.RestApi.Interfaces
{
    [ServiceContract]
    public interface ISiteService
    {
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "Sites/{customerId}/Organizations/", 
            BodyStyle = WebMessageBodyStyle.Bare,
            RequestFormat = WebMessageFormat.Xml,
            ResponseFormat = WebMessageFormat.Xml)]
        EntityList<Organization> GetOrganizationsById(string customerId, string ids);
    }
}

with this service implementation: 通过此服务实现:

public SiteServiceClass{
    public EntityList<Organization> GetOrganizationsById(string customerId, string ids)
    {
        Authenticate();
        //Div Code
        var hierarchyCollection = GetHierarchies(customerId);
        var organizations = new EntityList<Organization>();
        foreach (Organization hierarchy in hierarchyCollection)
        {
            organizations.EntityArray.Add(hierarchy);
        }
        organizations.Total = organizations.EntityArray.Count;
        return organizations;
    }
}

using System.Collections.Generic;
using System.Runtime.Serialization;

namespace MyComp.RestApi.Entities.Utils
{
    /// <summary>
    /// Generic entity collection with sorting, paging and filtering.
    /// </summary>
    /// <typeparam name="T">Entity type.</typeparam>
    [DataContract(Name="EntityList")]
    public class EntityList<T> 
    {
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        public EntityList()
        {
            EntityArray = new List<T>();
        }

        /// <summary>
        /// Entity list.
        /// </summary>
        [DataMember]
        public List<T> EntityArray { get; set; }

        /// <summary>
        /// Totle count.
        /// </summary>
        [DataMember]
        public int Total { get; set; }

        /// <summary>
        /// Page number.
        /// </summary>
        [DataMember]
        public int CurrentPageIndex { get; set; }

        /// <summary>
        /// Page size.
        /// </summary>
        [DataMember]
        public int PageSize { get; set; }
    }
}

Trying to go to the service URL I get this exception: 尝试转到服务URL时遇到以下异常:

An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:
System.InvalidOperationException: An exception was thrown in a call to a WSDL export extension: System.ServiceModel.Description.DataContractSerializerOperationBehavior
 contract: http://tempuri.org/:ISiteService ----> System.InvalidOperationException: DataContract for type 'MyComp.RestApi.Entities.Utils.EntityList`1[[MyComp.RestApi.Entities.Organization, MyComp.RestApi.Entities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=85d7c8ed1eb8be9b]]' cannot be added to DataContractSet since type 'MyComp.RestApi.Entities.Utils.EntityList`1[[MyComp.RestApi.Entities.Site, MyComp.RestApi.Entities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=85d7c8ed1eb8be9b]]' with the same data contract name 'EntityList' in namespace 'Bugg' is already present and the contracts are not equivalent.
   at System.Runtime.Serialization.DataContractSet.Add(XmlQualifiedName name, DataContract dataContract)
   at System.Runtime.Serialization.DataContractSet.Add(Type type)
   at System.Runtime.Serialization.XsdDataContractExporter.Export(Type type)
   at System.ServiceModel.Description.MessageContractExporter.ExportType(Type type, String partName, String operationName, XmlSchemaType& xsdType)
   at System.ServiceModel.Description.DataContractSerializerMessageContractExporter.ExportBody(Int32 messageIndex, Object state)
   at System.ServiceModel.Description.MessageContractExporter.ExportMessage(Int32 messageIndex, Object state)
   at System.ServiceModel.Description.MessageContractExporter.ExportMessageContract()
   at System.ServiceModel.Description.DataContractSerializerOperationBehavior.System.ServiceModel.Description.IWsdlExportExtension.ExportContract(WsdlExporter exporter, WsdlContractConversionContext contractContext)
   at System.ServiceModel.Description.WsdlExporter.CallExtension(WsdlContractConversionContext contractContext, IWsdlExportExtension extension)
   --- End of inner ExceptionDetail stack trace ---
   at System.ServiceModel.Description.WsdlExporter.CallExtension(WsdlContractConversionContext contractContext, IWsdlExportExtension extension)
   at System.ServiceModel.Description.WsdlExporter.CallExportContract(WsdlContractConversionContext contractContext)
   at System.ServiceModel.Description.WsdlExporter.ExportContract(ContractDescription contract)
   at System.ServiceModel.Description.WsdlExporter.ExportEndpoint(ServiceEndpoint endpoint, XmlQualifiedName wsdlServiceQName)
   at System.ServiceModel.Description.WsdlExporter.ExportEndpoints(IEnumerable`1 endpoints, XmlQualifiedName wsdlServiceQName)
   at System.ServiceModel.Description.ServiceMetadataBehavior.MetadataExtensionInitializer.GenerateMetadata()
   at System.ServiceModel.Description.ServiceMetadataExtension.EnsureInitialized()
   at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.InitializationData.InitializeFrom(ServiceMetadataExtension extension)
   at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.GetInitData()
   at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.TryHandleDocumentationRequest(Message httpGetRequest, String[] queries, Message& replyMessage)
   at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.ProcessHttpRequest(Message httpGetRequest)
   at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.Get(Message message)
   at SyncInvokeGet(Object , Object[] , Object[] )
   at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

Excerpts from web config: Web配置摘录:

<services>
<service behaviorConfiguration="MyComp.RestApi.Services.SiteServiceBehavior"
                name="MyComp.RestApi.Services.SiteService">
                <endpoint behaviorConfiguration="webHttp" binding="webHttpBinding"
                    contract="MyComp.RestApi.Interfaces.ISiteService" />
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            </service>
</services>
 <behaviors>
            <endpointBehaviors>
                <behavior name="webHttp">
                    <webHttp />
                </behavior>
            </endpointBehaviors>
            <serviceBehaviors>
<behavior name="MyComp.RestApi.Services.SiteServiceBehavior">
                  <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="true" />
                </behavior>

</serviceBehaviors>
</behaviors>

Remove the name from DataContract attribute. 从DataContract属性中删除名称。 You are using generic data contract but you are giving it static name. 您正在使用通用数据协定,但您为其指定了静态名称。 It is not possible because the contract name for each generic type argument has to be different (auto-generated). 这是不可能的,因为每个泛型类型参数的合同名称必须不同(自动生成)。 That is the reason for your exception. 那就是你例外的原因。

Also remove mex endpoint and service metadata behavior from your configuration file because it is for SOAP service, not for REST services. 还从配置文件中删除mex端点和服务元数据行为,因为它是针对SOAP服务而不是REST服务的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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