WCF已知类型错误

时间:2011-05-31 16:54:37

标签: wcf known-types

调用我的服务时出现此错误:

Server Error in '/' Application.
--------------------------------------------------------------------------------

Configuration Error 
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately. 

Parser Error Message: There was an error while trying to serialize parameter http://DSD.myCompany.net/DsdWebServices/2011/05/:config. The InnerException message was 'Type 'System.OrdinalComparer' with data contract name 'OrdinalComparer:http://schemas.datacontract.org/2004/07/System' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'.  Please see InnerException for more details.

Source Error: 


Line 130:            passwordAttemptWindow="10"
Line 131:            passwordStrengthRegularExpression=""
Line 132:            type="DsdWebsite.Providers.DsdMembershipProvider, DsdWebsite.Providers" />
Line 133:      </providers>
Line 134:    </membership>


Source File: C:\Development\DSD Website\WebUI\web.config    Line: 132 


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.5444; ASP.NET Version:2.0.50727.5420 

该服务是会员提供商的数据服务。我创建了一个MembershipUser DTO来在服务中来回移动数据。它仅使用标准类:string,int,DateTime。我使用Guid而不是providerUserKey的对象。

服务的界面如下所示:

[ServiceContract(Namespace = "http://DSD.myCompany.net/DsdWebServices/2011/05/")]
[ServiceKnownType(typeof(MembershipUserDTO))]
[ServiceKnownType(typeof(NameValueCollection))]
[ServiceKnownType(typeof(Guid))]
[ServiceKnownType(typeof(DateTime))]
public interface IDsdMembershipProviderService
{
    [OperationContract]
    void Initialize(string name, NameValueCollection config);

    [OperationContract]
    MembershipUserDTO CreateUser(string username, 
        string salt,
        string encodedPassword,
    ...

DTO看起来像这样

namespace DsdWebsite.Services.Providers
{
    [Serializable]
    [DataContract]
    [KnownType(typeof(Guid))]
    [KnownType(typeof(DateTime))]
    public class MembershipUserDTO
    {
        public MembershipUserDTO(string providerName, string userName, Guid providerUserKey, string email,
                              string passwordQuestion, string comment, bool isApproved, bool isLockedOut,
                              DateTime creationDate, DateTime lastLoginDate, DateTime lastActivityDate,
                              DateTime lastPasswordChangedDate, DateTime lastLockoutDate,
                              string firstName, string lastName, string cellPhone, string officePhone,
                              string brokerId, bool isAdmin, bool mustChangePassword)
        {
            ProviderName= providerName;
            UserName = userName;
            ProviderUserKey= providerUserKey;
            Email= email;
            PasswordQuestion= passwordQuestion;
            Comment= comment;
            IsApproved=isApproved;
            IsLockedOut= isLockedOut;
            CreationDate= creationDate;
            LastLoginDate= lastLoginDate;
            LastActivityDate= lastActivityDate;
            LastPasswordChangedDate = lastPasswordChangedDate;
            LastLockoutDate=lastLockoutDate;
...

最后,我的web.config看起来像这样:

<membership
 defaultProvider="DsdMembershipProvider"
 userIsOnlineTimeWindow="15"
 hashAlgorithmType="">   <providers>
     <clear/>
     <add
         name="DsdMembershipProvider"
         connectionStringName="DsdMembershipConnectionString"
         enablePasswordRetrieval="true"
         enablePasswordReset="true"
         requiresQuestionAndAnswer="true"
         applicationName="/DsdWebsite/"
         requiresUniqueEmail="true"
         passwordFormat="Encrypted"
         maxInvalidPasswordAttempts="5"
         minRequiredPasswordLength="7"
         minRequiredNonalphanumericCharacters="0"
         passwordAttemptWindow="10"
         passwordStrengthRegularExpression=""
         type="DsdWebsite.Providers.DsdMembershipProvider,
 DsdWebsite.Providers" />  
 </providers> </membership>

如何确定导致错误的类型或对象? 感谢

1 个答案:

答案 0 :(得分:2)

使用以下ServiceKnownTypeAttribute构造函数指定包含将返回服务已知类型的静态方法declaringType的类类型(methodName):

public ServiceKnownTypeAttribute(
    string methodName,
    Type declaringType
)

在前面提到的静态方法中添加已添加的所有服务已知类型(虽然我认为没有DateTimeGuid会做得很好),并且还添加System.OrdinalComparer。 / p>

问题是System.OrdinalComparer是内部类,因此您必须通过反射获取类型。

修改

System.OrdinalComparermscorlib汇编的一部分。基本上你可以通过以下方式获得它的类型:

Type[] types = typeof( string ).Assembly.GetTypes();

然后您可以按名称检索所需类型(使用Linq,添加必要的using语句)。

Type type = types.Where( x => x.FullName == "System.OrdinalComparer" );

前两行可以合二为一,为简单起见,使用两行。

如果您需要更多详细信息,请说出来。