这是我的 WCF 服务的接口:
[ServiceContract]
public interface IMyService
{
[OperationContract]
void DoWork();
[OperationContract]
void SendData();
[OperationContract]
void GetData();
[OperationContract]
void GetUpdate();
}
public class StateObject
{
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 20;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public StringBuilder sb = new StringBuilder();
}
.cs 文件的代码是这样的:
public void GetUpdate()
{
StartListening();
}
public static void StartListening()
{
// Establish the local endpoint for the socket.
// The DNS name of the computer
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 1200);
// Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Bind the socket to the local endpoint and listen for incoming connections.
try
{
listener.Bind(localEndPoint);
listener.Listen(200);
while (true)
{
// Set the event to nonsignaled state.
allDone.Reset();
// Start an asynchronous socket to listen for connections.
listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);
// Wait until a connection is made before continuing.
allDone.WaitOne();
}
}
catch (Exception e)
{
}
}
第一个问题是我收到超时错误。
TimeoutException:对“ http://localhost:17263/MyService.svc ”的 HTTP 请求已超过分配的超时时间 00:01:00。分配给此操作的时间可能是较长超时的一部分。
第二个错误是。
WebException:操作已超时
这是我的web.config
:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="IncreasedTimeout" sendTimeout="00:05:00" />
<binding name="BasicHttpBinding_IMyService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint name="BasicHttpBinding_IMyService"
address="http://localhost:17263/MyService.svc"
binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IMyService"
contract="MyServiceRef.IMyService" />
</client>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
</system.serviceModel>
我在 application_start() 中调用服务
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
MyServiceRef.MyServiceClient myService = new MyServiceRef.MyServiceClient();
Thread thread = new Thread(() => myService.GetUpdate());
thread.Start();
Application["ClientObj"] = myService;
SqlDependency.Start(constr);
//trythis.connection.ReceiveData();
}
我没有遇到我上面提到的任何问题。因为现在我遇到了有史以来最大的问题-
每当我使用 WCF 服务参考运行我的 Web 应用程序时,我的 VS2015 和我的计算机都会挂起。没有反应,一切都冻结了。没有任何效果。为什么会这样?
更新: 我搜索了这个问题,它说没有异常捕获,这就是系统冻结的原因。所以我使用了 try catch 块。现在显示了我的网页,但随后系统也冻结了。响应几乎为零。我现在能做什么。我读到它发生在 tcp 连接上,但如何解决呢?