3

当我在 Azure 辅助角色中安装了以下包时:

  <package id="Microsoft.AspNet.WebApi.Client" version="5.1.0" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.1.0" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi.OData" version="5.1.0" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi.Owin" version="5.1.0" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi.OwinSelfHost" version="5.1.0" targetFramework="net451" />
  <package id="Microsoft.Data.Edm" version="5.6.0" targetFramework="net451" />
  <package id="Microsoft.Data.OData" version="5.6.0" targetFramework="net451" />
  <package id="Microsoft.Data.Services.Client" version="5.6.0" targetFramework="net451" />
  <package id="Microsoft.Owin" version="2.0.2" targetFramework="net451" />
  <package id="Microsoft.Owin.Host.HttpListener" version="2.0.2" targetFramework="net451" />
  <package id="Microsoft.Owin.Hosting" version="2.0.2" targetFramework="net451" />
  <package id="Microsoft.WindowsAzure.ConfigurationManager" version="2.0.3" targetFramework="net451" />
  <package id="Newtonsoft.Json" version="5.0.8" targetFramework="net451" />
  <package id="Owin" version="1.0" targetFramework="net451" />
  <package id="RavenDB.Client" version="2.5.2750" targetFramework="net451" />
  <package id="System.Spatial" version="5.6.0" targetFramework="net451" />
  <package id="Unity" version="3.0.1304.1" targetFramework="net451" />
  <package id="Unity.WebAPI" version="5.1" targetFramework="net451" />
  <package id="WindowsAzure.Storage" version="3.0.2.0" targetFramework="net451" />

只要我不尝试注册 IoC 容器,我就可以让应用程序正常工作。

只要我把这行放在我的 Startup.cs 文件中:

config.DependencyResolver = new UnityDependencyResolver(new UnityContainer());

我得到以下异常:

Microsoft.WindowsAzure.ServiceRuntime.dll 中出现“System.Reflection.TargetInvocationException”类型的未处理异常

如果我将完全相同的代码放在控制台应用程序中,它可以正常工作而不会引发异常。这是我的 WorkerRole.cs 类的全部内容:

    public class WorkerRole : RoleEntryPoint
    {
        IDisposable _app;

        public override void Run()
        {
            Trace.TraceInformation("Timbre.Catalogue.ApiRole entry point called");

            while (true)
            {
                Thread.Sleep(10000);
                Trace.TraceInformation("Working");
            }
        }

        public override bool OnStart()
        {
            ServicePointManager.DefaultConnectionLimit = 12;

            var endpoint = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["ApiEndpoint"];
            var baseUri = string.Format("{0}://{1}", endpoint.Protocol, endpoint.IPEndpoint);
            Trace.TraceInformation("Starting OWIN at {0}", baseUri);
            _app = WebApp.Start<Startup>(new StartOptions(baseUri));

            return base.OnStart();
        }

        public override void OnStop()
        {
            if (_app != null)
                _app.Dispose();

            base.OnStop();
        }
    }

她的就是我的 Startup.cs 课程的全部内容:

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            var config = new HttpConfiguration();
            config.Formatters.Remove(config.Formatters.XmlFormatter);
            config.MapHttpAttributeRoutes();
            config.DependencyResolver = new UnityDependencyResolver(new UnityContainer());

            app.UseWebApi(config);
        }
    }

尝试将 Castle.Windsor 与以下软件包一起使用时,我遇到了同样的问题:

<package id="WebApiContrib.IoC.CastleWindsor" version="0.10.0.0" targetFramework="net451" />

而这行代码:

config.DependencyResolver = new WindsorResolver(new WindsorContainer().Install(FromAssembly.This()));

谁能告诉我这里有什么问题?我怀疑这是 Azure 计算模拟器的问题,因为它可以在控制台应用程序中完美运行,但是我不知道如何进一步诊断它。

4

1 回答 1

3

nuget 包5.1 版本Unity.WebApi参考5.0Web API 版本构建。所以我们需要一个绑定重定向来使 Unity 与5.1Web API 版本一起工作。

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.1.0.0" newVersion="5.1.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

与您稍后关于找出异常详细信息的评论相关:

您可以查看 Web API 5.1 中新的全局错误处理功能。以下是其中的一个片段。

config.Services.Add(typeof(IExceptionLogger), new CustomExceptionLogger());

public class CustomExceptionLogger : ExceptionLogger
{
    public override void Log(ExceptionLoggerContext context)
    {
        //TODO: Log the exception details
        //context.Request
        //context.Exception
    }
}
于 2014-01-24T14:56:17.893 回答