2015-06-12 43 views
0

我有完全不使用Newtonsoft.Json库HttpSelfHostServer程序,但我得到以下错误:我不使用Newtonsoft.Json,但我得到一个错误说,它不能被发现

Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified. 

at System.Net.Http.Formatting.JsonMediaTypeFormatter..ctor() 
at System.Net.Http.Formatting.MediaTypeFormatterCollection.CreateDefaultFormatters() 
at System.Net.Http.Formatting.MediaTypeFormatterCollection..ctor() 
at System.Web.Http.HttpConfiguration.DefaultFormatters() 
at System.Web.Http.HttpConfiguration..ctor(HttpRouteCollection routes) 
at System.Web.Http.SelfHost.HttpSelfHostConfiguration..ctor(Uri baseAddress) 
at System.Web.Http.SelfHost.HttpSelfHostConfiguration..ctor(String baseAddress) 

我的代码:

using System; 
using System.Web.Http; 
using System.Web.Http.SelfHost; 
using System.Runtime.InteropServices; 
using System.IO; 

var selfHostConfiguraiton = new HttpSelfHostConfiguration("http://localhost:8080"); <- Error 

selfHostConfiguraiton.Routes.MapHttpRoute(
    name: "DefaultApiRoute", 
    routeTemplate: "endpoints/{controller}", 
    defaults: null); 

using (var server = new HttpSelfHostServer(selfHostConfiguraiton)) 
{ 
    Console.WriteLine("Server started"); 
    server.OpenAsync().Wait(); 
} 

我确实在其他项目中使用Json.NET,但没有这个。

错误发生在这条线

var selfHostConfiguraiton = new HttpSelfHostConfiguration("http://localhost:8080"); 

并且在app.config中它没有提及。

感谢您的建议。

+3

WebAPI内部使用'Json.NET'。 –

回答

1

JSON.NET是ASP.NET Web API的组成部分。它有一些DataContractJsonSerializer不支持的功能,比如它可以序列化的类型以及它们应该如何序列化的灵活性更高。你可以在下面的网页中找到如何在Web API中使用link

+0

谢谢你的建议apros。我已经开始使用Json.NET,它很棒。我的问题是在我安装Json.NET之前对我以前的程序进行了重大更改,这些程序运行良好;( –

2

谢谢你们我找到了解决方案。添加到您的app.config

<runtime> 
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
    <dependentAssembly> 
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/> 
    <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/> 
    </dependentAssembly> 
</assemblyBinding> 

而且你需要有Newtonsoft.Json.dll在输出目录。

编辑: 显然从Nuget安装Json.NET是对旧WebApi自托管项目的重大改变,您将需要引用Newtonsoft.Json.dll以使其工作。

您还需要参考下列DLL: System.Net.Http.dll,System.Net.Http.Formatting.dll,System.Web.Http.dll,System.Web.Http.SelfHost.dll

祝你有美好的一天:)

相关问题