C # The error "Basic Connection closed: An error occurred while receiving" occurs when WebService is called.

Source: Internet
Author: User

Problem description: C # remote backup of Oracle Database by calling WebService. When the backup data volume reaches GB or more, the error "Basic Connection closed: An error occurred while receiving" occurs, data backup fails.

Note: WebService has been asynchronously called.

 

Reference:

0. C # The basic connection has been closed: the connection was accidentally closed, the wrong solution (original address: http://0e2.net/post/1173.html)

 

Debug a form that uses httpwebrequest to simulate submissionProgramThe above error prompt frequently appears. Google found several solutions.
1) add it to the application. config or web. config file.

<System.net>
<Settings>
<Httpwebrequest useunsafeheaderparsing = "true"/>
</Settings>
</System.net>

2) set the clientconnectionlimit attribute in the. config file of the client:
For example:

<System. runtime. remoting>
<Application>
<Channels>
<Channel ref = "HTTP" clientconnectionlimit = "50">
<Clientproviders>
<Formatter ref = "Soap"/>
</Clientproviders>
</Channel>
</Channels>
</Application>
</System. runtime. remoting>

Both methods did not solve my problem. I suddenly saw someone talking about the headers information.Code

Myrequest. useragent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2;. Net CLR 1.0.3705 ;)";
Solve the problem.

1. http://www.dingkao.com/a/100802/356147/

Use vs 2003 to add an automatic WebServices object for Web reference.
However, an error occurs when calling a method that requires a lot of time.
Error cause:
"An Unexpected error occurred when the basic connection has been disabled. "
I have set the timeout value to the maximum, that is
WebServices object. Timeout =-1;
Please advise, what should I do when calling a method that requires a long wait.
WebServices can be accessed in the browser?
The WebServices generated by Asp.net has a debugging page, which can be executed directly by inputting three values, once this method is executed for a long time, an error indicating that the page cannot be displayed will be returned. use callback to avoid timeout

2. ASP. NET Server-side asynchronous web method

Abstract: Matt Powell describes how to use Asynchronous web methods on the server to create a high-performance Microsoft ASP. NET web service.
Introduction

In the third column in March, I talked about the use of Microsoft? The client function of. NET Framework asynchronously calls web services through HTTP. This method is very useful for calling Web Services, so you do not have to lock your application or generate too many background threads during use. Now let's take a look at the asynchronous web method that provides similar functions on the server side. The asynchronous web method has a high performance similar to the hse_status_pending method in writing ISAPI extensions. However, you do not need to write code for managing your own thread pool, and it also has all the advantages of running managed code. First, let's consider the general synchronization of Microsoft? ASP. NET web method. When you return a result from a synchronous web method, a response is sent to the method. If it takes a long time to complete the request, the request processing thread will be occupied until the method call ends. Unfortunately, most long calls are caused by long database queries or calls to another web service. For example, if you call the database, the current thread will wait until the call is completed. The thread has nothing to do, but waits until the query is returned. When the thread waits for the completion of the call to the TCP socket or backend web service, a similar problem occurs.

It is not good to keep the thread in the waiting state, especially when the server is under heavy pressure. The waiting thread does not perform any valid work, such as providing services for other requests. We need to find a way to start a long background process on the server and return the current thread to the ASP. NET process pool. Then, when a long background process is completed, we call a callback function to end processing the request and notify ASP. NET that the request has been completed in some way. In fact, this function can be provided by ASP. NET using asynchronous web methods.

Working principle of asynchronous web method

Microsoft? Visual Studio? . Net only compiles your code to create an assembly. When you receive a request for its web method, the Assembly is called. The Assembly itself does not know anything about soap. Therefore, when your application is started for the first time, the asmx handler must reflect your Assembly to determine which web methods are provided. For conventional synchronous requests, these operations are simple: Find out which methods have the associated webmethod attributes, and set the logic to call the correct method based on the soapaction HTTP header.

For asynchronous requests, the asmx processing program looks for a web method that has a certain signature and recognizes the signature as Asynchronous. This handler will look for method pairs that comply with the following rules:

Beginxxx and endxxx web methods. XXX is an arbitrary string that indicates the name of the method to be provided.
The beginxxx function returns an iasyncresult interface and accepts asynccallback and an object as the last two input parameters.
The endxxx function accepts an iasyncresult interface as its unique parameter.

Both methods must be identified using the webmethod attribute.

If the asmx handler finds that two methods meet all the preceding conditions, the method XXX is provided in its WSDL as a conventional web method. This method uses parameters defined before the asynccallback parameter in the beginxxx signature as input and returns the content returned by the endxxx function. Therefore, if a web method has the following synchronous declaration:
[Webmethod]
Public String lengthyprocedure (INT milliseconds ){...}
 

The asynchronous declaration will be:

[Webmethod]
Public iasyncresult beginlengthyprocedure (
Int milliseconds,
Asynccallback CB,
Object s ){...}

[Webmethod]
Public String endlengthyprocedure (iasyncresult call ){...}
 

The WSDL of each method is the same.

After the asmx handler reflects the Assembly and detects an asynchronous web method, it must process requests for this method in a different way than processing synchronous requests. It calls the beginxxx method instead of a simple method. It restores and serializes incoming requests to the parameters to be passed to the function (the same as when processing synchronous requests ); but it also passes the pointer to an internal callback function (as an additional asynccallback parameter of the beginxxx method ).

This method is similar to the asynchronous programming mode of the web service client application in. NET Framework. If the client supports asynchronous web service calls, the threads occupied by the client computer can be released. If the server supports asynchronous web service calls, the threads occupied by the server computer can be released. But there are two key differences. First, the beginxxx and endxxx functions are not called by the server code, but by the asmx handler. Second, you need to write code for beginxxx and endxxx functions, instead of using the code generated by the Add web reference (add web reference) wizard in WSDL. EXE or Visual Studio. NET. But the results are the same, that is, release the thread so that it can execute other processes.

After the asmx handler calls the beginxxx function of the server, it returns the thread to the thread pool of the process so that it can process any other received requests. However, the httpcontext of the request cannot be released. The asmx handler waits until the callback function passed to the beginxxx function is called and ends processing the request.

Once the callback function is called, The asmx handler calls the endxxx function so that your web method can complete any processing to be executed and get the returned data serialized into the soap response. After the endxxx function returns, a response is sent. Only the httpcontext of the request is released.
Simple asynchronous web method

To illustrate asynchronous web methods, I started with a simple synchronous web method named lengthyprocedure. The Code is as follows. Then let's take a look at how to complete the same task asynchronously. Lengthyprocedure only takes a specified number of milliseconds.
[WebService]
Public class syncwebservice: system. Web. Services. WebService
{
[Webmethod]
Public String lengthyprocedure (INT milliseconds)
{
System. Threading. thread. Sleep (milliseconds );
Return "successful ";
}
}
 

Now we can convert lengthyprocedure to an asynchronous web method. We must create the beginlengthyprocedure function and endlengthyprocedure function as described above. Remember that an iasyncresult interface is returned for our beginlengthyprocedure call. Here, I plan to use a delegate and the begininvoke method on the delegate to make our beginlengthyprocedure call Asynchronous Method call. The callback function passed to beginlengthyprocedure will be passed to the ininvoke method on the delegate, and the iasyncresult returned from begininvoke will be returned by the beginlengthyprocedure method.

When the delegate is completed, the endlengthyprocedure method is called. We will call the endinvoke method on the delegate to pass in iasyncresult and use it as the input for the endlengthyprocedure call. The returned string is the string returned from the web method. The following is the code:

[WebService]
Public class asyncwebservice: system. Web. Services. WebService
{
Public Delegate string lengthyprocedureasyncstub (
Int milliseconds );

Public String lengthyprocedure (INT milliseconds)
{
System. Threading. thread. Sleep (milliseconds );
Return "successful ";
}

Public class mystate
{
Public object previusstate;
Public lengthyprocedureasyncstub asyncstub;
}

[System. Web. Services. webmethod]
Public iasyncresult beginlengthyprocedure (INT milliseconds,
Asynccallback CB, object S)
{
Lengthyprocedureasyncstub stub
= New lengthyprocedureasyncstub (lengthyprocedure );
Mystate MS = new mystate ();
Ms. previusstate = s;
Ms. asyncstub = stub;
Return stub. begininvoke (milliseconds, CB, MS );
}

[System. Web. Services. webmethod]
Public String endlengthyprocedure (iasyncresult call)
{
Mystate MS = (mystate) Call. asyncstate;
Return Ms. asyncstub. endinvoke (CALL );
}
}
 

When to use the asynchronous web method

When determining whether the asynchronous web method is suitable for your application, consider several issues. First, the beginxxx function must return an iasyncresult interface. Iasyncresult is returned from multiple asynchronous I/O operations, including accessing data streams and performing Microsoft? Windows? SOCKET call, execution file I/O, interaction with other hardware devices, and Asynchronous Method call, including calling other Web Services. You can obtain iasyncresult from these asynchronous operations to return it from the beginxxx function. You can also create your own class to implement the iasyncresult interface, but you may need to encapsulate an I/O operation mentioned above in some way.

For most asynchronous operations mentioned above, it is meaningful to use the asynchronous web method to encapsulate backend asynchronous calls, which can make Web Service Code more effective. However, this does not apply to asynchronous method calls using delegation. Delegation will cause Asynchronous Method calls to occupy a thread in the thread pool of the process. Unfortunately, the asmx handler also uses these threads when providing services for incoming requests. Therefore, unlike calling for real I/O operations on hardware or network resources, calling using a delegated Asynchronous Method still occupies one of the Process threads during execution. You can also use the original thread to run your web method synchronously.

The following example shows an asynchronous web method that calls the backend web service. It has used the webmethod attribute to identify the begingetage and endgetage methods for asynchronous operation. The code of this asynchronous web method calls the backend web method named userinfoquery to obtain the information it needs to return. The call to userinfoquery is asynchronously executed and passed to the asynccallback function. The latter is passed to the begingetage method. This will call the internal callback function when the backend request is complete. The callback function then calls the endgetage method to complete the request. The code in this example is much simpler than the code in the previous example and has another advantage, that is, it does not start backend processing in the same thread pool as providing services for the middle-layer web method requests.

[WebService]
Public class getmyinfo: system. Web. Services. WebService
{
[Webmethod]
Public iasyncresult begingetage (asynccallback CB, object state)
{
// Call an asynchronous web service call.
Localhost. userinfoquery proxy
= New localhost. userinfoquery ();
Return proxy. begingetuserinfo ("User Name ",
CB,
Proxy );
}

[Webmethod]
Public int endgetage (iasyncresult res)
{
Localhost. userinfoquery proxy
= (Localhost. userinfoquery) res. asyncstate;
Int age = proxy. endgetuserinfo (RES). Age;
// Perform other operations on the Web service results here
// Process.
Return age;
}
}
 

One of the most common I/O operations that occur in web methods is the call to the SQL database. Unfortunately, what is Microsoft? Ado. Net has not yet defined a sound asynchronous call mechanism, but it does not help to improve efficiency by packaging SQL calls to asynchronous delegate calls. Although you can choose to cache the results, you should also consider using Microsoft SQL Server 2000 Web Services Toolkit (English) to publish your database as a web service. In this way, you can use the support in. NET Framework to asynchronously call web services to query or update databases.

Pay attention to a large number of backend resources when accessing SQL through web service calls. If you use a TCP socket to communicate with a Unix computer, or use a dedicated database driver to access other available SQL platforms, or even resources accessed using DCOM, you can consider using many web service toolkit to publish these resources as Web Services.

One of the advantages of using this method is that you can take advantage of the client's Web service structure, such as the use of. NET Framework asynchronous web service calls. In this way, you will obtain the asynchronous call capability for free, and your client access mechanism will work with the asynchronous web method efficiently.

Aggregate Data Using asynchronous web methods

Currently, many Web Services Access Multiple backend resources and aggregate information for front-end web services. Although calling multiple backend resources increases the complexity of the asynchronous web method model, the efficiency can be significantly improved.
Assume that your web method calls two backend Web Services: Service A and service B. From your beginxxx function, you can call services a and services B asynchronously. You should pass your own callback function to each asynchronous call. After receiving the results from service a and service B, to trigger the completion of the web method, the callback function you provide will verify that all requests have been completed, perform all processing on the returned data, and then call the callback function passed to the beginxxx function. This will trigger the call to the endxxx function, and the return of this function will lead to the completion of the asynchronous web method.

Summary

asynchronous web methods provide an effective mechanism in ASP. NET web services to call backend services without occupying valuable threads in the process thread pool. By combining asynchronous requests to backend resources, the server can use its own web method to maximize the number of requests simultaneously processed. You should consider using this method to develop high-performance Web service applications.
http://msd2d.com/newsletter_tip.aspx? Section = DOTNET & id = DeleGate
I define asynccallback DeleGate
private asynccallback callbackhandler;
initialization must be in the form's instance function (preferably constructor)
callbackhandler = new asynccallback (mycallback);
(your own instance is defined in this way.
private asynccallback callbackhandler = new asynccallback (mycallback ); compilation and translation failed to find a long reason
but still do not understand the principle that caused the problem
2. Use the ininvoke method of the proxy to call the proxy and bind a callback function
targethandler. begininvoke (callbackhandler, nothing)
the return value of the proxy is transmitted as a parameter to asynccallback delegate callbackhandler
the definition of mycallback must be as follows
private void mycallback (iasyncresult IAR)
the getmermershandler method is getcustomershandler. endinvoke (IAR);
the mycallback function is used to process things that need to be done
In short, asynchronous calls are implemented through asynccallback commissioned by FCL.
This article is from the csdn blog, reprint please indicate the source: http://blog.csdn.net/shanyou/archive/2004/07/02/32706.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.