从WCF服务序列化AspNet.Identity对象

时间:2014-11-30 12:59:10

标签: c# .net wcf

我现在正处于疯狂的状态,所以这里......

我将紧密耦合的登录/用户管理系统与MVC5分开并将其粘贴在我自己的用户管理服务中,这在通过DLL引用服务时非常有用(仅用于测试)。但是,当涉及到适当的SOA并引用此服务时,我得到:

Type 'Microsoft.AspNet.Identity.UserLoginInfo' cannot be serialized

辉煌。所以我一直在尝试去除其他对象的序列化错误,但仍然坚持这个。

这是唯一使用它的地方:

 public LoginResult AddLogin(string userId, LoginInfo info)
 {
     var createdUser = UserManager.AddLogin(userId, new UserLoginInfo(info.LoginProvider, info.ProviderKey));

     return new LoginResult
     {
         Errors = createdUser.Errors.ToList(),
         Succeeded = createdUser.Succeeded
     };

正如你所看到的,我不能不使用它,但我不是在服务之间传递它 - >客户端,所以我不知道为什么编译器想要序列化它。

我试过了:

[ServiceKnownType(typeof(UserLoginInfo))]

我的服务也没有运气。我甚至无法添加[DataContract],因为它是另一个库的一部分,或者创建一个新对象并从UserLoginInfo继承,因为AddLogin需要该对象。

当然必须有解决方案吗?

修改

我要求的转化课程:

namespace UserManagement.Core.Service
{
    [DataContract]
    public class LoginResult
    {
        [DataMember]
        public bool Succeeded { get; set; }

        [DataMember]
        public List<string> Errors { get; set; }
    }

    [DataContract]
    public class LoginInfo
    {
        [DataMember]
        public string ProviderKey { get; set; }

        [DataMember]
        public string LoginProvider { get; set; }
    }

}

服务配置:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
  </configSections>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
  </appSettings>
  <connectionStrings>
    // Removed
  </connectionStrings>
  <!--
    For a description of web.config changes see http://go.microsoft.com/fwlink/?LinkId=235367.

    The following attributes can be set on the <httpRuntime> tag.
      <system.Web>
        <httpRuntime targetFramework="4.5.3" />
      </system.Web>
  -->
  <system.web>
    <compilation debug="true" targetFramework="4.5.3"/>
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https"/>
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
    </providers>
  </entityFramework>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

SVC标记:

namespace UserManagement.Core.Service
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public partial class UserManagementService : IUserManagementService
    {
        private readonly IUnitOfWorkFactory<ApplicationDbContext> _factory;

        UnitOfWork<ApplicationDbContext> _context;

        /// <summary>
        /// AssociationService Constructor
        /// </summary>
        /// <param name="factory"></param>
        public UserManagementService(IUnitOfWorkFactory<ApplicationDbContext> factory)
        {
            _factory = factory;

            // Use the context sent from the web application
            if (OperationContext.Current != null)
            {
                string dbContextName = OperationContext.Current.IncomingMessageHeaders.GetHeader<string>("DatabaseContext", "");
                _context = this._factory.Create(dbContextName);
            }
            else
            {
                _context = this._factory.Create();
            }
        }
    }
}

0 个答案:

没有答案