简体   繁体   中英

Error in Retrieving Objects from WCF Service

New to WCF Services and needs some help.

So I have a client (basic console app) to test the WS (which is hosted on a test server not localhost)

With it I can pass and retrieve standard objects like string and int to and fro.

I can also pass custom classes to the WS but not retrieve them. I get either timeout or

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.

I searched about endpoints and every single one seemed different and not obviously adaptable I am currently using the stock web.config when the project was created below which I think is where my issue is but finding difficult to know what to put.

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>

Service Method in Question

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();
    }
}

EDIT

Database Connection Test (Works as expected)

 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;
            }
        }

I'm using the translate call to convert EF objects to prevent exposure as from what I can understand is bad.

Class

[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; }
}

Interface

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

Test Console

 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);
            }


        }

** Solved.

This issue was a parameter I left out for brevity was actually the cause, it wasn't serialised and thus rejected.

原因是另一个参数“ Request_Items”,该参数未在其类中与DataContract序列化。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM