使用Owin将Service Fabric项目部署到Azure Service Fabric Cluster

时间:2017-09-21 06:46:51

标签: azure-service-fabric service-fabric-stateless

我正在与Owin合作开发一个Service Fabric项目,而且我在将其部署到云中时遇到了麻烦。我已经搜索了同样问题的其他人,但我只找到了一个答案,告诉集群中的错误告诉代码中出错的地方。我已经关注了如何编写失败的方法的微软Owin教程,但没有运气。 我可以从Visual Studio直接在Localhost上运行该项目,但是当我将它部署到Azure中的Service Fabric集群时,问题就出现了。我有一个5节点集群正在运行,当我部署它时,它会在2分钟后开始发出警告,并在5分钟后发出错误。应用程序的状态是" inbuild"。 Image of warningImage of error

我有两个服务,来自我的集群的错误在这两个方法中给出了错误(每个服务中的方法相同(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 Cluster而不会出现任何错误。祝你有个愉快的一天,感谢他们的帮助。

2 个答案:

答案 0 :(得分: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 Listener实例的实例。这是我的代码

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

这种方法对我有用。尝试使用它希望它也适合你

答案 1 :(得分: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))
        //};
    }