【问题标题】:ASP .NET Core 2.0 deployment unable to connect with WCF service in productionASP .NET Core 2.0 部署无法与生产中的 WCF 服务连接
【发布时间】:2019-10-08 03:49:43
【问题描述】:

我有一个连接 WCF 服务的 .NET 核心应用程序,在开发环境中一切正常,但在部署环境中服务没有连接。 我发布了 ASP .NET 核心项目并将其托管在 IIS 中,发布的应用程序还包含 ConnectedService.json 文件:

{
  "ProviderId": "Microsoft.VisualStudio.ConnectedService.Wcf",
  "Version": "15.0.20628.921",
  "GettingStartedDocument": {
    "Uri": "https://go.microsoft.com/fwlink/?linkid=858517"
  },
  "ExtendedData": {
    "Uri": "http://localhost:8759/Design_Time_Addresses/CECWcfServiceLib/CECService/text/mex",
    "Namespace": "CEC_WCF_Service",
    "SelectedAccessLevelForGeneratedClass": "Public",
    "GenerateMessageContract": false,
    "ReuseTypesinReferencedAssemblies": true,
    "ReuseTypesinAllReferencedAssemblies": true,
    "CollectionTypeReference": {
      "Item1": "System.Array",
      "Item2": "System.Runtime.dll"
    },
    "DictionaryCollectionTypeReference": {
      "Item1": "System.Collections.Generic.Dictionary`2",
      "Item2": "System.Collections.dll"
    },
    "CheckedReferencedAssemblies": [],
    "InstanceId": null,
    "Name": "CEC_WCF_Service",
    "Metadata": {}
  }
}

我得到的错误是

System.NullReferenceException: Object reference not set to an instance of an object.
   at AspNetCore.Views_Home_CEC_Dashboard.ExecuteAsync() in D:\Users\sajja\source\repos\CECDashboard\CECDashboard\Views\Home\CEC_Dashboard.cshtml:line 9
   at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page, ViewContext context)
   at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page, ViewContext context, Boolean invokeViewStarts)
   at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context)
   at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, String contentType, Nullable`1 statusCode)
   at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ActionContext actionContext, IView view, ViewDataDictionary viewData, ITempDataDictionary tempData, String contentType, Nullable`1 statusCode)
   at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.ExecuteAsync(ActionContext context, ViewResult result)
   at Microsoft.AspNetCore.Mvc.ViewResult.ExecuteResultAsync(ActionContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultAsync(IActionResult result)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResultFilterAsync[TFilter,TFilterAsync]()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResultExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultFilters()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

我是否缺少部署配置,因为这在开发环境中运行良好。

[更新] HomeController 中的代码

public ActionResult CEC_Dashboard()
        {
            try
            {
                CEC_WCF_Service.CECServiceClient cECService = new CEC_WCF_Service.CECServiceClient();
                var task1 = Task.Run(async () => await cECService.OpenAsync());
                task1.Wait();

                var task2 = Task.Run(async () => await cECService.GetAccountsAsync());
                task2.Wait();
                var accountsInfo = task2.Result.ToList();

                var task3 = Task.Run(async () => await cECService.GetAccountStatsOnAccountAsync(1));
                task3.Wait();
                var account_Stats = task3.Result.ToList();

                ViewData["Account_Stats"] = account_Stats;
                ViewData["accountsInfo"] = accountsInfo;
                ViewData["DefaultAccountsView"] = accountsInfo.Find(o => o.AccountId == 1);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return View();
        }

【问题讨论】:

  • 基于ConnectedService.json,连接http://localhost:8759。您是否在http://localhost:8759 下发布了您的 WCF 服务或运行了 WCF 服务?此外,对于这个错误,它发生在Home\CEC_Dashboard.cshtml:line 9。与我们分享有关它的更多信息。
  • 是的,WCF 服务正在运行,我也可以导航到该页面。发生的错误是因为在 HOME 控制器中的页面启动时,我正在调用该函数以从 WCF 服务获取信息以显示在页面上。我已经在原始帖子中添加了我正在调用的代码。顺便说一句,这一切都在开发环境中工作,但不知何故在生产中中断。
  • 对于这个错误,似乎ViewData["Account_Stats"]ViewData["accountsInfo"]ViewData["DefaultAccountsView"] 之一为空。尝试记录它们的值。另外,你的异常是视图抛出的,在视图中访问之前尝试添加空检查。

标签: wcf asp.net-core


【解决方案1】:

所以,经过几天的研究,我发现了问题所在。和最初的想法一样,问题确实与生产部署的 WCF 配置有关。

所以,这是我在研究多线程后所做的。

1- 在 HomeController 中注入 IConfiguration

public class HomeController : Controller
    {
        private IConfiguration configuration;

        public HomeController(IConfiguration iConfig)
        {
            configuration = iConfig;
        }
  <...More Class Code here ...>
 }

2- 在 appsettings.json 中添加设置

"MyService": {
    "EndpointUrl": "http://localhost:8082/hello?wsdl"
  }

3- 这是进行生产部署时缺少的内容。 Reference.cs 文件有开发时地址的地址,所以我做了以上所有措施来缓解并能够在运行时加载生产地址。

此方法在Reference Post的一篇帖子中引用

//
                var EndPoint = configuration.GetSection("MyService").GetSection("EndpointUrl").Value;
                System.ServiceModel.BasicHttpBinding result = new System.ServiceModel.BasicHttpBinding();
                result.MaxBufferSize = int.MaxValue;
                result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
                result.MaxReceivedMessageSize = int.MaxValue;
                result.AllowCookies = true;
                //

                CEC_WCF_Service.CECServiceClient cECService = new CEC_WCF_Service.CECServiceClient(result, new System.ServiceModel.EndpointAddress(EndPoint));
                var task1 = Task.Run(async () => await cECService.OpenAsync());
                task1.Wait();

这解决了生产部署的问题!

【讨论】:

    猜你喜欢
    • 2019-06-05
    • 1970-01-01
    • 2019-11-24
    • 2018-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多