小编Dan*_*-NP的帖子

C# 将对象转换为带有类型参数的通用接口

我想将一个对象转换为通用自定义接口。

这是我的界面:

public interface IContainer<T>
{
    IEnumerable<T> SaveToCache(IEnumerable<T> models);
}

public class Container<T>
{
    IEnumerable<T> SaveToCache(IEnumerable<T> models);
}
Run Code Online (Sandbox Code Playgroud)

我在服务类中使用这个接口:

public class Manager()
{
    public Manager()
    {
        address = new Container<Address>(this);
        // more containers are constructed here...
    }

    private static Container<Address> address;
    public static Container<Address> Address => address;

    // here are more properties of Type Container<T>
}
Run Code Online (Sandbox Code Playgroud)

现在我想像SaveToCacheAsync这样动态调用该方法:

private void SaveItems(IEnumerable<object> items, string typeName)
{
    object container = typeof(Manager)
        .GetRuntimeProperty(typeName).GetValue(null, null);
    var t = Type.GetType($"MyNameSpace.Models.{typeName}");

    // variant A - …
Run Code Online (Sandbox Code Playgroud)

c# generics reflection

6
推荐指数
1
解决办法
4942
查看次数

使用Web API从JQuery请求令牌

我在Javascript中执行ajax请求以从我的WebAPI AuthenticationProvider获取JWT.这是我的js函数:

    function authenticateUser(credentials) {
    var body = {
        grant_type: 'password',
        client_id: 'myClientId',
        client_secret: 'myClientSecret',
        username: credentials.name,
        password: credentials.password
    };

    $.ajax({
        url: 'http://localhost:9000/token',
        type: 'POST',
        dataType: 'json',
        contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
        /* data: JSON.stringify(body), /* wrong */
        data: body, /* right */
        complete: function(result) {
            //called when complete
            alert(result);
        },

        success: function(result) {
            //called when successful
            alert(result);
        },

        error: function(result) {
            //called when there is an error
            alert(result);
        },
    });
    session.isAuthenticated(true);
    return true;
}
Run Code Online (Sandbox Code Playgroud)

虽然内容以正确的方式发送给我,但OAuthValidateClientAuthenticationContext只有空值.

从控制台复制我的表单数据:

{ "grant_type": "密码", …

authentication jquery jwt asp.net-web-api2

5
推荐指数
1
解决办法
4588
查看次数

Web API 异常包装器

我正在使用 WebAPI 调用一些第三方方法:

public class SessionsController : ApiController
{
    public DataTable Get(int id)
    {
        return Services.TryCall(es => es.GetSessionList(id).Tables[0]);
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在包装此服务的所有调用:

internal static class Services
{
    internal static IExternalService ExternalService { get; set; }

    internal static T TryCall<T>(Func<IExternalService,T> theFunction)
    {
        try
        {
            return theFunction(ExternalService);
        }
        catch (FaultException<MyFaultDetail> e)
        {
            var message = new HttpResponseMessage(HttpStatusCode.NotImplemented);
            message.Content = new StringContent(e.Detail.Message);
            throw new HttpResponseException(message);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当抛出异常时,它会被捕获并准备好消息。通过重新抛出,我收到一条新的错误消息:

处理 HTTP 请求导致异常。有关详细信息,请参阅此异常的 'Response' 属性返回的 HTTP 响应。

如何在没有 Visual Studio 抱怨的情况下正确返回此异常?当我跳过错误时,浏览器会得到 501 错误结果。该方法Request.CreateResponse()在我的包装方法中不可用。

c# exception-handling httpresponse asp.net-web-api

2
推荐指数
1
解决办法
2093
查看次数