2014年7月13日 星期日

[RESOLVED] calling a wcf services multiple times


Hello guys,


I have a page that displays different values from different method from a single WCF Service.


At this point I am calling the WCF twice in the different private method of the code behind. like below




   private void showCRMRequest()
{
rDB = new RegistrationDB(_ctx);
string loggedInEmail = HttpContext.Current.User.Identity.Name;
var contactPlatformID = rDB.GetTaxEntityByEmail(loggedInEmail).TaxEntityPlatformId;
CRMIntegrationServiceClient client = new CRMIntegrationServiceClient();
var myRequestCount = client.GetRequests(contactPlatformID);
var myRequestMsgs = client.GetRequests(contactPlatformID).OrderByDescending(rd=>rd.CreatedDate).Take(4);
lblNoOfRequests.Text = myRequestCount.Count().ToString();
if (myRequestMsgs.Count() != 0)
{
StringBuilder sb = new StringBuilder();
sb.Append("
    ");
    foreach (var requestMsgs in myRequestMsgs)
    {

    sb.Append("" + MyZimraHelpers.TruncateAtWord(requestMsgs.Subject, 50) + "
    ");

    }

    myRequestContainer.InnerHtml = sb.ToString();
    }
    else
    {
    myRequestContainer.InnerHtml = "No Request was found";
    dvMyRequestVwMore.Visible = false;
    }
    client.Close();
    }



    private void showCRMHistory()
    {
    rDB = new RegistrationDB(_ctx);
    string loggedInEmail = HttpContext.Current.User.Identity.Name;
    var contactPlatformID = rDB.GetTaxEntityByEmail(loggedInEmail).TaxEntityPlatformId;
    CRMIntegrationServiceClient client = new CRMIntegrationServiceClient();
    var myRequestCount = client.GetRequests(contactPlatformID);
    var myRequestMsgs = client.GetRequests(contactPlatformID).OrderByDescending(rd=>rd.CreatedDate).Take(4);
    lblNoOfRequests.Text = myRequestCount.Count().ToString();
    if (myRequestMsgs.Count() != 0)
    {
    StringBuilder sb = new StringBuilder();
    sb.Append("

Is there a way of instantiating the call once and then make sure the object the closed at the end of each call.



Thanks



Are you looking for synchronous call,
URL
can help


It says;



  • It defines a 
    private
    boolean type instance variable "
    AsynchronousCall
    ", which will be initiated in the constructor of the class. This boolean variable will be used by the event handling method "
    WCFServiceCall_Click
    " to decide if a synchronous call or an asynchronous call should be made to the WCF service when the "Make WCF service call" button is clicked.

  • If a synchronous call should be made, the method "
    MakeSynchronousCall
    " is used. It will issue a synchronous WCF call and bind the list of the student information from the WCF service to the "DataGrid".

  • If an asynchronous call should be made, the method "
    MakeAsynchronousCall
    " is used. It will issue an asynchronous call and bind the list of the student information from the WCF service to the "DataGrid" in the
    "AsyncCallback" "delegate".

Both synchronous and asynchronous calls to the WCF service are pretty simple by using the proxies generated by the Visual Studio. When making a synchronous call, the UI thread that issues the call will be blocked until the WCF service sends the results back
to the client. When an asynchronous call is made, the UI thread sends the WCF request and forks a worker thread to wait for the response from the WCF service. The UI thread is not blocked and remains responsive to other user interactions. When the response
from the WCF service comes back, the "AsyncCallback" "delegate" is executed to process the WCF service
response.


沒有留言:

張貼留言