0

我正在使用 Owin 开发 Service Fabric 项目,但在将其部署到云中时遇到了麻烦。我搜索了其他有同样问题的人,但我只找到了一个答案,告诉集群中的错误告诉代码哪里出错了。我按照 Microsoft 的 Owin 教程介绍了如何编写失败的方法,但没有运气。我可以从 Visual Studio 直接在 Localhost 上运行该项目,但是当我将它部署到 Azure 中的 Service Fabric 群集时,问题就开始了。我有一个 5 节点集群正在运行,当我部署到它时,它会在 2 分钟后开始发出警告,并在 5 分钟后发出错误。应用程序的状态是“inbuild”。 警告图像和错误图像

我有两个服务,我的集群中的错误给出了这两种方法中的错误(每个服务中的相同方法(OpenAsync)):

        public Task<string> OpenAsync(CancellationToken cancellationToken)
    {
        var serviceEndpoint =
            _parameters
            .CodePackageActivationContext
            .GetEndpoint("ServiceEndpoint");

        var port = serviceEndpoint.Port;
        var root =
            String.IsNullOrWhiteSpace(_appRoot)
            ? String.Empty
            : _appRoot.TrimEnd('/') + '/';


        _listeningAddress = String.Format(
            CultureInfo.InvariantCulture,
            "http://+:{0}/{1}",
            port,
            root
        );
        _serverHandle = WebApp.Start(
            _listeningAddress,
            appBuilder => _startup.Configuration(appBuilder)
        );

        var publishAddress = _listeningAddress.Replace(
            "+",
            FabricRuntime.GetNodeContext().IPAddressOrFQDN
        );

        ServiceEventSource.Current.Message("Listening on {0}", publishAddress);
        return Task.FromResult(publishAddress);
    }

来自集群的错误告诉错误在本节中:

          _serverHandle = WebApp.Start(
            _listeningAddress,
            appBuilder => _startup.Configuration(appBuilder)
        );

另一种方法(来自其他服务):

        public Task<string> OpenAsync(CancellationToken cancellationToken)
    {
        var serviceEndpoint =
            _parameters
            .CodePackageActivationContext
            .GetEndpoint("ServiceEndpoint");

        var port = serviceEndpoint.Port;
        var root =
            String.IsNullOrWhiteSpace(_appRoot)
            ? String.Empty
            : _appRoot.TrimEnd('/') + '/';

        _listeningAddress = String.Format(
            CultureInfo.InvariantCulture,
            "http://+:{0}/{1}",
            port,
            root
        );

        try
        {
            _serverHandle = WebApp.Start(
                _listeningAddress,
                appBuilder => _startup.Configuration(appBuilder)
            );
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw e;
        }


        var publishAddress = _listeningAddress.Replace(
            "+",
            FabricRuntime.GetNodeContext().IPAddressOrFQDN
        );

        ServiceEventSource.Current.Message("Listening on {0}", publishAddress);
        return Task.FromResult(publishAddress);
    }

来自集群的错误告诉错误在本节中:

            try
        {
            _serverHandle = WebApp.Start(
                _listeningAddress,
                appBuilder => _startup.Configuration(appBuilder)
            );
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw e;
        }

我的创业课程:

        public void Configuration(IAppBuilder appBuilder)
    {
        var corsAttr = new EnableCorsAttribute(origins: "*", headers: "*", methods: "*");

        var config = new HttpConfiguration();
        config.WithWindsorSetup();
        config.WithJsonSetup();
        config.MapHttpAttributeRoutes(); //Enable Attribute-routing
        config.WithSwaggerSetup();
        config.EnsureInitialized();
        config.EnableCors(corsAttr);
        appBuilder.UseWebApi(config);


    }

我在哪里创建了一个新的 OwenCommunicationListener:

        protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
    {
        return new[] {
            new ServiceInstanceListener(initParams => new OwinCommunicationListener("", new Startup.Startup(), initParams))
        };
    }

我非常希望能够将它部署到 Azure Service Fabric 集群而不会出现任何错误。祝您有美好的一天,感谢您的帮助。

4

2 回答 2

0

问题解决了。我编辑了这段代码:

        protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
    {
        return Context.CodePackageActivationContext.GetEndpoints()
            .Where(endpoint => endpoint.Protocol.Equals(EndpointProtocol.Http) || endpoint.Protocol.Equals(EndpointProtocol.Https))
            .Select(endpoint => new ServiceInstanceListener(serviceContext => new OwinCommunicationListener("", new Startup.Startup(), serviceContext)));

        //return new[] {
        //    new ServiceInstanceListener(initParams => new OwinCommunicationListener("", new Startup.Startup(), initParams))
        //};
    }
于 2017-09-21T09:36:15.810 回答
0

您需要编写自己的自定义类来配置 Owin 侦听器的路由和 http 配置。这是我用来配置路由的类,请尝试使用它:

    /// <summary>
    /// This is the startup class that configure the routing and http configuration for Owin listener.
    /// </summary>
    public static class Startup
        {
        // This code configures Web API. The Startup class is specified as a type
        // parameter in the WebApp.Start method.
        public static void ConfigureApp (IAppBuilder appBuilder)
            {

            appBuilder.UseCors(CorsOptions.AllowAll);
            // Configure Web API for self-host. 
            HttpConfiguration config = new HttpConfiguration();
            config.MapHttpAttributeRoutes();

            var json = config.Formatters.JsonFormatter;
            json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None;
            config.Formatters.Remove(config.Formatters.XmlFormatter);

            appBuilder.UseWebApi(config);
            }
        }

将此类作为操作传递给您正在创建 OwinCommunication 侦听器实例的实例。这是我的代码

endpoints.Select(endpoint => new ServiceInstanceListener(
                serviceContext => new OwinCommunicationListener(Startup.ConfigureApp, serviceContext,
                    null, endpoint), endpoint));

这种方法对我有用。尝试一下,希望它也对你有用

于 2017-09-21T07:10:15.250 回答