相关疑难解决方法(0)

使ASP.NET WCF将字典转换为JSON,省略"Key"和"Value"标记

这是我的困境.我正在使用RESTful ASP.NET服务,尝试使用以下格式返回JSON字符串的函数:

{"Test1Key":"Test1Value","Test2Key":"Test2Value","Test3Key":"Test3Value"}
Run Code Online (Sandbox Code Playgroud)

但我会以这种格式得到它:

[{"Key":"Test1Key","Value":"Test1Value"},
{"Key":"Test2Key","Value":"Test2Value"},
{"Key":"Test3Key","Value":"Test3Value"}]
Run Code Online (Sandbox Code Playgroud)

我的方法看起来像这样:

[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public Dictionary<string, string> Test(String Token)
{
    if (!IsAuthorized(Token))
        return null;

    if (!IsSecure(HttpContext.Current))
        return null;

    Dictionary<string, string> testresults = new Dictionary<string, string>();
    testresults.Add("Test1Key", "Test1Value");
    testresults.Add("Test2Key", "Test2Value");
    testresults.Add("Test3Key", "Test3Value");
    return testresults;
}
Run Code Online (Sandbox Code Playgroud)

有没有办法只使用内置的ASP.NET工具摆脱那些"Key"和"Value"标签?(即,如果可以避免的话,我宁愿不使用JSON.NET)

非常感谢!:)

c# asp.net rest wcf json

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

替换WCF默认的JSON序列化

是否可以替换WCF的默认JSON序列化(我目前正在测试该webHttp行为),并application/json作为MIME类型传递.特别是,我不喜欢默认情况下每个属性都是键/值对,如:

{"Key":"PropertyName", "Value":"PropertyValue"}
Run Code Online (Sandbox Code Playgroud)

我只在支持JSON的端点上使用该服务(使用jQuery + WCF请求数据).

wcf serialization json

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

使用自定义WCF正文反序列化而不更改URI模板反序列化

这篇博文中,我能够创建一个IDispatchMessageFormatter使用JSON.NET序列化的自定义WCF .它有一点需要注意:使用它UriTemplate并不一定按预期工作.

这是博客文章提供的实现:

class NewtonsoftJsonDispatchFormatter : IDispatchMessageFormatter
{
    private readonly OperationDescription od;
    private readonly ServiceEndpoint ep;
    private readonly Dictionary<string, int> parameterNames = new Dictionary<string, int>();

    public NewtonsoftJsonDispatchFormatter(OperationDescription od, ServiceEndpoint ep, bool isRequest)
    {
        this.od = od;
        this.ep = ep;
        if (isRequest)
        {
            int operationParameterCount = od.Messages[0].Body.Parts.Count;
            if (operationParameterCount > 1)
            {
                this.parameterNames = new Dictionary<string, int>();
                for (int i = 0; i < operationParameterCount; i++)
                {
                    this.parameterNames.Add(od.Messages[0].Body.Parts[i].Name, i);
                }
            }
        }
    }
    public void …
Run Code Online (Sandbox Code Playgroud)

c# wcf json json.net

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

WCF 中的动态 ExpandoObject

尝试通过枚举类型进程和动态数据的操作来 Ping/Pong 我的服务。

[ServiceContract ( CallbackContract = typeof ( iStackoverflowCallBack ) )]
public interface iStackoverflow
{
    [OperationContract]
    void Ping ( Process Operation , dynamic Data );
}

[ServiceContract ( )]
public interface iStackoverflowCallBack
{
    [OperationContract]
    void Pong ( Process Operation , dynamic Data );
}
Run Code Online (Sandbox Code Playgroud)

为什么这个服务有连接问题?

  • 在实现两个接口时dynamic自动转换为object.
  • ping荷兰国际集团从消费我的服务,ping没有在所有到达服务,但该服务是否工作正常。

解决方案 :

[DataContract]
public class SerializableDynamicObject : IDynamicMetaObjectProvider
{
    [DataMember]
    private IDictionary<string,object> dynamicProperties = new Dictionary<string,object>();

    #region IDynamicMetaObjectProvider implementation
    public DynamicMetaObject GetMetaObject (Expression expression)
    { …
Run Code Online (Sandbox Code Playgroud)

c# wcf dynamic

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

标签 统计

wcf ×4

c# ×3

json ×3

asp.net ×1

dynamic ×1

json.net ×1

rest ×1

serialization ×1