![](/img/trans.png)
[英]Inject an IEnumerable of generic interface in Unity Dependency Injection c#
[英]C# (Web API) dependency injection (with Unity) return type Interface
我正在使用ASP.NET構建WebApi。 現在,我將整個應用程序分為幾層,其中兩層是“接口”和“模型”(每一層都有其自己的.dll)。 在“ models.dll”中,定義了我所有的模型,並在應用程序需要的地方開始注入它們。 到現在為止還挺好。 現在,當我從控件返回值時,我面臨一個問題。 讓我們假設以下內容:
public class TestController : ApiController
{
IBusinessObject _businessObject;
public TestController(IBusinessObject businessObject)
{
this._businessObject= businessObject;
}
[Route("details/object/{id}")]
[HttpGet]
public ISomeObject GetObjectByNr(int id)
{
return this._businessObject.SearchForObject(id);
}
}
現在,當您查看GetObjectByNr時,您會看到它的返回類型是ISomeObject
(當然,this._businessObject.SearchForObject(id)的返回類型也是ISomeObject).ISomeObject的實際實現在ModelLayer內部,但是我不想將其包含在我的WebApi項目中,因為它shuold僅通過接口進行通信。 現在,當我像這樣運行它時,出現一個錯誤,提示我應該使用DataContractResolver
(以及有關序列化的內容)。 我知道我無法序列化接口,但我認為我的應用程序足夠聰明,可以在此處找到合適的類(一開始我會解析所有接口,並且在調用this._businessObject.SearchForObject(id)
時可以正常工作正確的類被調用並填充了正確的值,但是僅當此時返回對象時,我得到了錯誤)。 我一直在看SO,發現了一些“ KnownType(typeof(SomeObject))
”,例如KnownType(typeof(SomeObject))
屬性。 但是,當我嘗試在TestController中進行設置時,它無法被識別(但是即使可以工作,它也需要引用我的實際模型類,但我不希望這樣)。
有什么方法可以使我的接口可序列化並返回正確的對象,而無需實際告訴控制器ISomeObject的實現是什么?
謝謝
編輯:
嘗試使用自己的MediaTypeFormatter
。 您可以使用SharpSerializer或Json.NET。 有信息如何執行此操作:
http://www.asp.net/web-api/overview/formats-and-model-binding/media-formatters
http://www.asp.net/web-api/overview/advanced/configuring-aspnet-web-api
編輯注意:以下代碼針對MVC控制器,而不針對WebAPI ApiController。
如果您未綁定到DataContractSerializer
,則可以使用其他一些東西,例如json序列化。 例如,您可以使用Json.NET。 最方便的方法是覆蓋默認的Json結果行為:
public class JsonNetResult : JsonResult
{
public JsonSerializerSettings SerializerSettings { get; set; }
public Formatting Formatting { get; set; }
public JsonNetResult()
{
Formatting = Formatting.None;
SerializerSettings = new JsonSerializerSettings();
JsonRequestBehavior = JsonRequestBehavior.DenyGet;
}
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
throw new ArgumentNullException("context");
if (JsonRequestBehavior == JsonRequestBehavior.DenyGet
&& String.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
{
throw new InvalidOperationException("This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet.");
}
HttpResponseBase response = context.HttpContext.Response;
response.ContentType = !string.IsNullOrEmpty(ContentType)
? ContentType
: "application/json";
if (ContentEncoding != null)
response.ContentEncoding = ContentEncoding;
if (Data != null)
{
var writer = new JsonTextWriter(response.Output) { Formatting = Formatting };
var serializer = JsonSerializer.Create(SerializerSettings);
serializer.Serialize(writer, Data);
writer.Flush();
}
}
然后在您的控制器類中重寫Json方法:
protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding)
{
return new JsonNetResult
{
ContentType = contentType,
ContentEncoding = contentEncoding,
Data = data
};
}
protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior)
{
return new JsonNetResult
{
ContentType = contentType,
ContentEncoding = contentEncoding,
Data = data,
JsonRequestBehavior = behavior
};
}
並像這樣使用它:
public ActionResult GetObjectByNr(int id)
{
return Json(this._businessObject.SearchForObject(id), JsonRequestBehavior.AllowGet);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.