小编ven*_*mit的帖子

HttpResponseMessage作为Json返回什么

我有一个关于Web Api基础知识的基本问题.仅供参考,我之前已经查过但是找不到我要找的东西.

我有一段代码如下所述.就像任何其他方法一般来说我的方法叫做:Post,它必须返回一些东西,例如JSON,我该怎么做.具体来说,我应该在"return"之后写一下,以便将3个字段(loginRequest.Username,loginRequest.Password,loginRequest.ContractItemId)作为Json.Coments:不要担心用户名,密码和contractID在评论中,我确实在我的LinQ中获得了它们的价值.这只是我现在回归的问题,向所有想对此发表一些笔记的人致以问候.

    [System.Web.Http.HttpPost]
    public HttpResponseMessage Post(LoginModel loginRequest)
    {
        //loginRequest.Username = "staw_60";
        //loginRequest.Password = "john31";
        //loginRequest.ContractItemId = 2443;

      try
        {
           Membership member =
                (from m in db.Memberships
                 where
                     m.LoginID == loginRequest.Username 
                 && m.Password == loginRequest.Password 
                 && m.ContractItemID == loginRequest.ContractItemId
                 select m).SingleOrDefault();   
        }
       catch (Exception e)
       {
            throw new Exception(e.Message);
       }

      return ???;      
    }
Run Code Online (Sandbox Code Playgroud)

c# json asp.net-web-api

11
推荐指数
2
解决办法
5万
查看次数

配置NLog的布局以显示所有事件上下文条目

我目前正在开发webapi2应用程序,并在使用NLog进行日志的阶段.

在我的应用程序中,我以这种方式使用LogEventInfo.Properties字典以键值方式登录:

thisController.LogParams.Add("ControllerName",controllerName);
thisController.LogParams.Add("ActionName", actionName);
thisController.LogParams.Add("TotalTime", actionWatch.ElapsedMilliseconds);
LogEventInfo logEvent = new LogEventInfo() 
       { 
           Message = string.IsNullOrEmpty(thisController.LogMessage)? "Finished Successfuly":thisController.LogMessage,
           Level = LogLevel.Info,
           TimeStamp = DateTime.Now
       };
logEvent.Properties.Merge(thisController.LogParams);
logger.Log(logEvent);
Run Code Online (Sandbox Code Playgroud)

一切正常,但我似乎无法找到渲染布局的方式,因此它打印LogEventInfo.Properties字典中的所有键值条目.

假设我的目标是一个文件,那么我必须明确提到密钥名称,有没有办法渲染它来显示字典的所有内容?这是我今天如何做到这一点,我只能记录我所知道的条目:

<target name="f1" 
  xsi:type="File" 
  fileName="${basedir}\logs\log.txt" 
  maxArchiveFiles="60" 
  archiveNumbering="Date" 
  archiveDateFormat="yyyyMMdd" 
  archiveEvery="Day" 
  layout="${longdate} : ${callsite:className=true:methodName=true} : ${event-context:item=ControllerName} : ${event-context:item=ActionName} : ${event-context:item=TotalTime} : ${message}" />
Run Code Online (Sandbox Code Playgroud)

c# nlog

8
推荐指数
1
解决办法
2359
查看次数

简单的Injector GetAllInstances用Caliburn Micro抛出异常

我曾在Simple Injector和Caliburn micro工作,但已经有近2年了.现在,当我尝试创建一个简单的WPF应用程序时.首先,我最终阅读了文档,因为两个库都进行了大量的更改.

我遇到了一些问题,比如"查看无法找到",以后会得到解决,但现在我遇到了一个奇怪的问题.尝试启用记录器和一切,但不知道它是Caliburn微型或简单喷射器的问题.

这是我的bootstrapper类:

 internal class AppBootstrapper : BootstrapperBase
    {
        public static readonly Container ContainerInstance = new Container();

        public AppBootstrapper()
        {
            LogManager.GetLog = type => new DebugLogger(type);
            Initialize();
        }

        protected override void Configure()
        {
            ContainerInstance.Register<IWindowManager, WindowManager>();
            ContainerInstance.RegisterSingleton<IEventAggregator, EventAggregator>();

            ContainerInstance.Register<MainWindowViewModel, MainWindowViewModel>();

            ContainerInstance.Verify();
        }

        protected override void OnStartup(object sender, System.Windows.StartupEventArgs e)
        {
            DisplayRootViewFor<MainWindowViewModel>();
        }

        protected override IEnumerable<object> GetAllInstances(Type service)
        {
            // This line throwing is exception when running the application
            // Error: 
            // ---> An exception of type 'SimpleInjector.ActivationException' …
Run Code Online (Sandbox Code Playgroud)

c# wpf caliburn.micro simple-injector

4
推荐指数
1
解决办法
2034
查看次数

如何获取异常细节

我有一个WCF服务,我已经实现了自定义服务错误.我根据特定条件抛出一个错误,所以在if语句中我抛出下面的错误.

throw new FaultException<CustomServiceFault>(
    new CustomServiceFault
    {
        ErrorMessage = string.Format(
            "Error in response, code:{0} message:{1}", 
             response.Error.Code, 
             response.Error.Message),
        Source = "ConnectToOpenLdap Method",
        StackTrace = string.Format(
            "Error in response, code:{0} message:{1}", 
            response.Error.Code, 
            response.Error.Message)                                     
    },
    new FaultReason(
        string.Format(CultureInfo.InvariantCulture, "{0}", "Service fault exception")));
Run Code Online (Sandbox Code Playgroud)

在catch中,我重新抛出了这样的异常:

catch (Exception exception)
{
    var customServiceFault = GetCustomException(exception);
    throw new FaultException<CustomServiceFault>(
        customServiceFault, 
        new FaultReason(customServiceFault.ErrorMessage), 
        new FaultCode("Sender"));              
}   
Run Code Online (Sandbox Code Playgroud)

GetCustomException()方法只是将异常转换为我的自定义异常对象.

问题是传递给GetCustomException()的异常没有InnerException属性中的详细信息.我看到的截图:

在此输入图像描述

如何在if条件中抛出异常时提取或获取自定义的ErrorMessage,Source等?正如您在屏幕截图中看到的那样,扩展"exception"显示了对象类型(我相信),而"Detail"内部显示了ErrorMessage,InnerExceptionMesage,Source和StackTrace.这就是我所追求的.如何在GetCustomException()方法中提取这些值?

这是GetCustomException()方法:

private static CustomServiceFault GetCustomException(Exception exception)
{                       
    var customServiceFault = new CustomServiceFault
    {
        ErrorMessage = exception.Message,
        Source = exception.Source,
        StackTrace …
Run Code Online (Sandbox Code Playgroud)

c# wcf exception-handling

3
推荐指数
1
解决办法
1万
查看次数