【发布时间】: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