场景是; VS2017,MVC 5,StructureMap.MVC5
我收到以下错误:
构建类型BLL.MMCodes.MMCodesService时出错。” InnerException {“值不能为空。\ r \ n参数名称:字符串”}
尝试运行我的应用时出现在“ DoGetInstance”函数中。
protected override object DoGetInstance(Type serviceType, string key)
{
IContainer container = (CurrentNestedContainer ?? Container);
if (string.IsNullOrEmpty(key)) {
return serviceType.IsAbstract || serviceType.IsInterface
? container.TryGetInstance(serviceType)
: container.GetInstance(serviceType);
}
return container.GetInstance(serviceType, key);
}
服务和界面
namespace BLL.MMCodes
{
public interface IMMCodesService
{
bool ValidateAgainstBizRules(string MMCode, out string errorMessage);
bool _UseEF
{
get;
set;
}
}
public class MMCodesService : IMMCodesService
{
private string errorMessage;
public MMCodesService()
{
ValidateAgainstBizRules( MMCode, out errorMessage);
}
#region vars
public string MMCode { get; set; }
private bool IsValid { get; set; }
private List<string> Validations = new List<string>();
#endregion
public bool _UseEF { get; set; }
public bool ValidateAgainstBizRules(string mmCode, out string errorMessage)
{....}
控制器
using System.Collections.Generic;
using System.Web.Mvc;
using BLL.MMCodes;
using BusinessLayerDemoProject.Models;
namespace BusinessLayerDemoProject.Controllers
{
public class MMCodeController : Controller
{
private IMMCodesService _iService;
public MMCodeController(IMMCodesService service)
{
this._iService = service;
this._iService._UseEF = true;
}
我尝试了所有论坛的一些建议
例如。更改StructureMapDependencyScope
中的属性以适应HTTPContext和CurrentNestedContainer属性中的null,但没有成功。
答案 0 :(得分:1)
问题是此代码:
wrap_content
构造函数中的代码引发异常。因此Set objOApp2 = New Outlook.Application
Set objAppt2 = objOApp2.CreateItem(olAppointmentItem)
With objAppt2
.RequiredAttendees = recipient
.Subject = tsk
.Importance = 2 ' high
.Start = mydt
.End = mydt2
.Location = ""
.body = body1
.MeetingStatus = 1
.MeetingStatus = olMeeting
.ReminderMinutesBeforeStart = 120
.ReminderSet = True
.Actions("Reply").Enabled = False
.Actions("Reply to All").Enabled = False
.Actions("Forward").Enabled = False
.Send
End With
无法将其传递给依赖它的那些对象。
因此,您可以修改代码以使其不会引发异常。
但是 ...
我几乎可以确定代码应该被删除/注释掉。该代码在构造函数内部被调用。因此,根据定义,public MMCodesService()
{
ValidateAgainstBizRules( MMCode, out errorMessage);
}
尚未设置-因此对其进行任何形式的验证都是没有意义的。因此,我建议将代码更改为:
StructureMap