从WCF服务检索对象时出错

时间:2016-12-14 15:16:33

标签: c# web-services wcf object

WCF服务新手,需要一些帮助。

所以我有一个客户端(基本控制台应用程序)来测试WS(它托管在测试服务器而不是localhost上)

有了它,我可以传递和检索标准对象,如字符串和int来回。

我也可以将自定义类传递给WS但不检索它们。我得到超时或

An error occurred while receiving the http response to... this could be due
to the service endpoint binding not using the http protocol. this could also be due to an 
http request context being aborted by the server (possibly due the
the service shutting down) 

The underlying connection was closed. An unexpected error occurred on receive.

Unable to read data from the transport connection, an existing connection 
was forcibly closed by the remote host.

我搜索了端点,每个端点看起来都不一样,而且显然不具有适应性我在创建项目时正在使用股票web.config,我认为这是我的问题所在,但很难知道要放什么。 / p>

WS Web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <appSettings />
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <httpRuntime />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <directoryBrowse enabled="true" />
  </system.webServer>
  <connectionStrings>
    <add name="CNS" connectionString="xyz" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>

</configuration>

问题中的服务方法

public Request GetOrderByRef(string Ref)
{
    AssetStockEntity db = new AssetStockEntity();
    try
    {
        RequestEntity _requestef = new RequestEntity();
          _requestef = db.RequestEntities.FirstOrDefault(x => x.Incident == Ref);

        if (_requestef == null)
            return null;
        else
            return TranslateRequestEntityToRequest(_requestef);
    }
    catch (Exception ex)
    {
        string debug = ex.ToString();
    }
}

修改

数据库连接测试(按预期工作)

 public string GetOrderByRefTest(string Ref)
        {
            AssetStockEntity db = new AssetStockEntity();
            try
            {
                RequestEntity _requestef = new RequestEntity();
                _requestef = db.RequestEntities.FirstOrDefault(x => x.Incident == Ref);

                if (_requestef == null)
                    return null;
                else
                    return _requestef.Requested_By;
            }
            catch (Exception ex)
            {
                string debug = ex.ToString();

                return debug;
            }
        }

我正在使用translate调用转换EF对象以防止暴露,因为我能理解的是坏的。

[DataContract]
public class Request
{
    [DataMember]
    public int RId { get; set; }
    [DataMember]
    public string Incident { get; set; }
    [DataMember]
    public string Requested_By { get; set; }
    [DataMember]
    public string Authorised_By { get; set; }
    [DataMember]
    public virtual ICollection<Request_Items> Request_Items { get; set; }
}

接口

[ServiceContract]
public interface Iws
{
    [OperationContract]
    Request GetOrderByRef(string Ref);
}

测试控制台

 static void Main(string[] args)
        {

            ASM.IwsClient ws = new ASM.IwsClient();

            try
            {
                Console.WriteLine(ws.GetOrderByRef("ABC123").Requested_By);
                Thread.Sleep(5000);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                Thread.Sleep(500000);
            }


        }

**解决了。

这个问题是我为了简洁而遗漏的一个参数实际上是原因,它没有被序列化,因而被拒绝。

1 个答案:

答案 0 :(得分:0)

原因是另一个参数“Request_Items”,它没有在其类中使用DataContract进行序列化。