Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8110087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:37:53+00:00 2026-06-06T01:37:53+00:00

2 small parts to this question which hopefully will clear up some ambiguity for

  • 0

2 small parts to this question which hopefully will clear up some ambiguity for me.

Firstly, which is better for calling a WCF service?

using (var myService = new ServiceClient("httpBinding")){
     try{         
          var customerDetails = GetCustomerDetails();
          var results = myService.GetCustomerPurchases(customerDetails);
     }catch(Exception e){
          ......
     }
}

or

var myService = new ServiceClient("httpBinding");
try{
     var customerDetails = GetCustomerDetails();
     var results = myService.GetCustomerPurchases(customerDetails);
}catch(Exception e){
     .......
}

What I’m wondering is, should I always be wrapping my service call in a using block? Does IDisposable.Dispose() get called if the service throws an exception?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T01:37:59+00:00Added an answer on June 6, 2026 at 1:37 am

    Have a look at this question.

    Also you can create a couple of classes like

    public class ClientService<TProxy>
    {
        private static ChannelFactory<TProxy> channelFactory = new ChannelFactory<TProxy>("*");
    
        public static void SendData(Action<TProxy> codeBlock)
        {
            var proxy = (IClientChannel) channelFactory.CreateChannel();
            bool success = false;
    
            try
            {
                codeBlock((TProxy)proxy);
                proxy.Close();
                success = true;
            }
            finally
            {
                if (!success)
                {
                    proxy.Abort();
                }
            }
        }
    
        public static TResult GetData<TResult>(Func<TProxy, TResult> codeBlock)
        {
            var proxy = (IClientChannel) channelFactory.CreateChannel();
            bool success = false;
    
            TResult result;
            try
            {
                result = codeBlock((TProxy)proxy);
                proxy.Close();
                success = true;
            }
            finally
            {
                if (!success)
                {
                    proxy.Abort();
                }
            }
    
            return result;
        }
    }
    
    public class SomeAnotherService<TProxy>
    {
        public static bool SendData(Action<TProxy> codeBlock)
        {
            try
            {
                ClientService<TProxy>.SendData(codeBlock);
                return true;
            }
            catch(Exception ex)
            {
                //...
            }
            return false;
        }
    
        public static TResult GetData<TResult>(Func<TProxy, TResult> codeBlock)
        {
            TResult result = default(TResult);
            try
            {
                result = ClientService<TProxy>.GetData(codeBlock);
            }
            catch (Exception ex)
            {
                //...
            }
    
            return result;
        }
    }
    

    Sometimes it’s convenient. Here is an example of calling some service method.

    var someObj = SomeAnotherService<IMyService>.GetData(x => x.SomeMethod());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am asking this question as a small part of my question series regarding
Firstly, please don't dismiss this question - I'm aware it's an ugly situation but
In conjunction from my another question , and after changing this small part of
This is a continuation to this question which Oleg has answered. I have 2
I have been pondering writing this question for quite some time. I work for
I am building a small Android application which will add a cube on user's
This is mostly an xcode question. I have an objective-c header with an small
I'm aware of this question which mentions Boost's STATIC WARNING, but I'd like to
for me I will answer this question based on my experience. so if I'm
I have functions that contribute to small parts of a figure generation. I'm trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.