SQL Server 到 WCF 开发时出错

发布于 2024-12-20 08:02:06 字数 3069 浏览 1 评论 0原文

我正在测试一个数据库,该数据库有两个要公开的表(卫星和通道),因为我需要使用 WCF。幸运的是,我尝试了我所知道的一切和网上的所有方法,但我无法解决问题。

这是服务合同 IService.cs

[ServiceContract]
public interface IService
{
    [OperationContract]
    List<Satalite> SelectSatalite(int satNum);

    [OperationContract]
    List<Satalite> SataliteList();

    [OperationContract]
    List<Channel> ChannelList(int satNum);

    [OperationContract]
    String Sat(int satNum);

}

这是 Service.svc.cs 文件

public class Service : IService
{
   DataDbDataContext DbObj = new DataDbDataContext();

   public List<Satalite> SataliteList()
   {
        var satList = from r in DbObj.Satalites
                      select r;

        return satList.ToList();
   }

    public List<Satalite> SelectSatalite(int satNum)
    {
        var satList = from r in DbObj.Satalites
                      where r.SateliteID == satNum
                      select r;

        return satList.ToList();
    }

    public List<Channel> ChannelList(int satNum)
    {
        var channels = from r in DbObj.Channels
                       where r.SateliteID == satNum
                       select r;

        return channels.ToList();
    }


    public String Sat(int satNum)
    {
        Satalite satObj = new Satalite();

        satObj = DbObj.Satalites.Single(p => p.SateliteID == satNum);
        return satObj.Name;
    }
} 

每当我尝试运行前三个文件时,我在使用 测试它们时都会出错>wcftestclient.exe,最后一个没有任何问题。

底层连接已关闭:连接已关闭 没想到。

服务器堆栈跟踪:
在 System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException、HttpWebRequest 请求、HttpAbortReason abortReason)
在 System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan 超时)
在System.ServiceModel.Channels.RequestChannel.Request(消息消息, TimeSpan 超时)
在 System.ServiceModel.Dispatcher.RequestChannelBinder.Request(消息 消息、TimeSpan 超时)
在 System.ServiceModel.Channels.ServiceChannel.Call(字符串操作, Boolean oneway,ProxyOperationRuntime操作,Object[] ins, Object[] 输出,TimeSpan 超时)
在 System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall、ProxyOperationRuntime操作)
在 System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage 消息)

在 [0] 处重新抛出异常:
在 System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg、IMessage retMsg)
在 System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData,Int32 类型)位于 IService.SelectSatalite(Int32 satNum)
在 ServiceClient.SelectSatalite(Int32 satNum)

内部异常:底层连接已关闭:连接 意外关闭。
在 System.Net.HttpWebRequest.GetResponse()
在 System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan 超时)

我的理解是,如果我使用 .net 编译器使用的已知类型(例如 intstring)它将毫无问题地工作。幸运的是,我没有找到解决办法。

I am testing a DB that have two tables (Satellite and Channel) to be exposed as I need using WCF. fortunately, I tried everything I know and online for more that I week now and I can't solve the problem.

This is the service contract IService.cs

[ServiceContract]
public interface IService
{
    [OperationContract]
    List<Satalite> SelectSatalite(int satNum);

    [OperationContract]
    List<Satalite> SataliteList();

    [OperationContract]
    List<Channel> ChannelList(int satNum);

    [OperationContract]
    String Sat(int satNum);

}

And this is the Service.svc.cs file

public class Service : IService
{
   DataDbDataContext DbObj = new DataDbDataContext();

   public List<Satalite> SataliteList()
   {
        var satList = from r in DbObj.Satalites
                      select r;

        return satList.ToList();
   }

    public List<Satalite> SelectSatalite(int satNum)
    {
        var satList = from r in DbObj.Satalites
                      where r.SateliteID == satNum
                      select r;

        return satList.ToList();
    }

    public List<Channel> ChannelList(int satNum)
    {
        var channels = from r in DbObj.Channels
                       where r.SateliteID == satNum
                       select r;

        return channels.ToList();
    }


    public String Sat(int satNum)
    {
        Satalite satObj = new Satalite();

        satObj = DbObj.Satalites.Single(p => p.SateliteID == satNum);
        return satObj.Name;
    }
} 

Whenever I try to run the first three I got an error when testing them using wcftestclient.exe, the last one works with no issues.

The underlying connection was closed: The connection was closed
unexpectedly.

Server stack trace:
at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException
webException, HttpWebRequest request, HttpAbortReason abortReason)
at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan
timeout)
at System.ServiceModel.Channels.RequestChannel.Request(Message message,
TimeSpan timeout)
at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message
message, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action,
Boolean oneway, ProxyOperationRuntime operation, Object[] ins,
Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage
methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage
message)

Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage
reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&
msgData, Int32 type) at IService.SelectSatalite(Int32 satNum)
at ServiceClient.SelectSatalite(Int32 satNum)

Inner Exception: The underlying connection was closed: The connection
was closed unexpectedly.
at System.Net.HttpWebRequest.GetResponse()
at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan
timeout)

What I understand is that the error happens for the custom classes which are the DB tables, if I used a known type by the .net compiler (ex. int or string) it will work with no problems. Fortunately, I didn't find a solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

朦胧时间 2024-12-27 08:02:06

该错误似乎是两个原因之一:

  1. 超时,因为您返回了太多数据,例如,从数据库中选择数据需要很长时间才能完成服务方法时间

    或:

  2. 消息大小太大,因为您选择的数据太多,因此 WCF 通信在整个数据完成之前中止已返回

我的解决方案:

  • 不要选择表中的所有数据!仅返回您可以真正处理/显示的数据,例如 10 行、20 行或最多 100 行...

尝试此操作 - 如果您将方法更改为:

 public List<Satalite> SataliteList(int count)
 {
      var satList = (from r in DbObj.Satalites
                    select r).Take(count);

      return satList.ToList();
 }

您可以使用例如从 WCF 测试客户端调用此方法吗? 计数 = 10计数 = 50 ??

The error appears to be one of two reasons:

  1. a timeout since you're returning too much data, e.g. the selection of the data from the database takes too long for the service method to complete in time

    or:

  2. the message size is too large, because you're selecting too much data, and thus the WCF communication aborts before the whole data has been returned

My solution:

  • don't select all data from the tables! Return only as much data as you can really handle / display, e.g. 10 rows, 20 rows or a maximum of 100 rows....

Try this - if you change your method to:

 public List<Satalite> SataliteList(int count)
 {
      var satList = (from r in DbObj.Satalites
                    select r).Take(count);

      return satList.ToList();
 }

Can you call this from the WCF Test Client with e.g. count = 10 or count = 50 ??

浅语花开 2024-12-27 08:02:06

调整服务器和客户端的超时设置会对您有所帮助。

服务器端调整绑定元素的SendTimeout属性,在客户端调整绑定元素的RecieveTimeout属性。

谢谢,

Adjusting timeout settings on server and client side will help you.

Server Side adjust the SendTimeout attribute of binding element and on client side adjust the RecieveTimeout attribute of binding element.

Thanks,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文