0

我做了以下自定义方法

[WebGet]
public string GetAllCars()
{

    return "hello";

}

我正在从浏览器中使用此功能,它工作正常,但是当我通过服务引用执行方法通过 Web 应用程序使用时,它运行良好,但无法通过快速观看看到结果,查询操作响应在那里,但不知道如何从中获取字符串结果

这是代码:

ServiceReference1.SampleDBEntities uricontext = new ServiceReference1.SampleDBEntities(new Uri("http://localhost/website2/wcfservice1.svc"));
    uricontext.Credentials = System.Net.CredentialCache.DefaultCredentials;
    var proxycontext = uricontext.Execute<String>(new Uri("http://localhost/website2/wcfdataservice1.svc/GetAllCars"));
4

2 回答 2

0

要从 WCF 数据服务代理获取标量值,最好调用FirstOrDefault()结果;

    private T ExecuteScalar<T>(Uri uri)
    {
        return Execute<T>(uri).FirstOrDefault();
    }

用法:

public string GetAllCars()
{
    return ExecuteScalar<string>(
        "http://localhost/website2/wcfdataservice1.svc/GetAllCars");
}
于 2013-06-03T08:26:03.350 回答
0

为了得到结果“Strings”,我们需要像这样“foreach”结果变量:

 ServiceReference1.SampleDBEntities uricontext = new ServiceReference1.SampleDBEntities(new Uri("http://localhost/website2/wcfservice1.svc"));
        uricontext.Credentials = System.Net.CredentialCache.DefaultCredentials;
        var proxycontext = uricontext.Execute<String>(new Uri("http://localhost/website2/wcfdataservice1.svc/GetAllCars"));
      //  var s = proxycontext.ToString();
        foreach (var r in proxycontext)
        {
            Response.Write(r.ToString());
        } 

有效!现在问题已经解决了。

于 2013-06-02T12:50:04.670 回答