2

I have created a vsto application that calls a webservice. everything seems to work just fine, but i would like to extend the functionality to call the test service version of my production service. code snippet that works that calls my test service.

  //how do i change here to be dynamic?
 npfunctions.finfunctions service = new npfunctions.finfunctions();
            var Results = service.ValidateFoapal(index.ToArray(), fund.ToArray(), org.ToArray(), prog.ToArray(), acct.ToArray(), row.ToArray());

            /* if their are no error then return a "Y" for success.*/
            if (Results.Count() < 0) { return LocallErrorInd; }
            /*well we have encountered errors lets adjust the spreadsheet to notify the user.*/

            else{
                //REMOVE ANY VISUAL ERRORS
                Microsoft.Office.Interop.Excel.Range delRng = Globals.ThisAddIn.Application.Range["R:S"];
                delRng.Delete(XlDeleteShiftDirection.xlShiftToLeft);
                for (int i = 0; i < Results.Count(); i++)
                {//set the error indicator 
                     LocallErrorInd = "Y";

                    //account error:
                    if (Results[i].FVALJOR_FUND_WARNING == "Y") 
                       {
                           Microsoft.Office.Interop.Excel.Range WrkRng = Globals.ThisAddIn.Application.Range[Results[i].FVALJOR_ROW];
                           WrkRng.Offset[0, 17].Value2 = "Invalid Account"; 
                        }

i have seen this post How can I dynamically switch web service addresses in .NET without a recompile? but it requires me to change my config file i would really like to change the service variable to point to another location based on a variable and basically flip from prod to test on my command. as i see it right now it appears that i would have to duplicate the code but i know there has got to be a better way. i like it to be something like.

if (TestBtn.Checked == true)
            {
                npfunctions.finfunctions service = new npfunctions.finfunctions();
               Results = service.ValidateFoapal(index.ToArray(), fund.ToArray(), org.ToArray(), prog.ToArray(), acct.ToArray(), row.ToArray());

            }

            if (PrdBtn.Checked == true)
            {
                  prdFunctions.finfunctions service = new prdFunctions.finfunctions();
                 Results = service.ValidateFoapal(index.ToArray(), fund.ToArray(), org.ToArray(), prog.ToArray(), acct.ToArray(), row.ToArray());

            }

            /* if their are no error then return a "Y" for success.*/
            if (Results.Count() < 0) { return LocallErrorInd; }
Community
  • 1
  • 1
Miguel
  • 2,019
  • 4
  • 29
  • 53
  • You might consider changing the behavior of the web service to "dynamic" and set the URL programatically at run time. – David W Aug 12 '13 at 17:55
  • Thanks david, i already did the dynamic part now how do i set the URL programatically? I really dont want to use the app.config. i needed to be more robust then that. Can you tell me how? – Miguel Aug 12 '13 at 17:57
  • Miguel, the webservcie proxy should have a Url property you can set at runtime. There is more comprehensive detail provided in http://stackoverflow.com/questions/125399/how-can-i-dynamically-switch-web-service-addresses-in-net-without-a-recompile?lq=1 – David W Aug 12 '13 at 18:09
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Aug 12 '13 at 18:46

2 Answers2

1

Does your service object not have a URL property?

2nd option, you can use a config file transformation so you do not need to manually change the settings(after the intial setup of course).

npfunctions.finfunctions service = new npfunctions.finfunctions();
if (TestBtn.Checked == true)
{
    service.url="<testurl>";
}
else
{
  service.url="<produrl>";
}
    Results = service.ValidateFoapal(index.ToArray(), fund.ToArray(), org.ToArray(), prog.ToArray(), acct.ToArray(), row.ToArray());
Daniel M
  • 268
  • 1
  • 2
  • 11
  • daniel im not sure i understand you fully. I have added a reference to my test service and production service. I would like to use the prod when i want and test when i want. I i like to do it at runtime. – Miguel Aug 12 '13 at 18:10
  • When you instantiate your service, you should be able to set the Url property of that service, eg npfunctions.finfunctions svc = new npfunctions.finfunctions(); svc.Url = ""; – David W Aug 12 '13 at 18:20
  • Ignore the second option, I missed that you wanted the dynamic option. What David says here. It's also the second answer in you "how to" link(by Bad Bruce) I'm going to edit my answer to show psuedo code – Daniel M Aug 12 '13 at 18:25
  • thank you daniel it appears that what setting the url property of the service worked. – Miguel Aug 12 '13 at 19:08
0

In our test client we have a drop-down to select between dev or tst. We also have buttons to select proxy or net.tcp. (We have many different people using our service using different methods).

In the app.config the names of the endpoints correlate with different selectable options. Eg. name="BasicHttpBinding_IInterface_PROXY_DEV"

You can then dynamically build up which endpoint you would like to use and go with that.

nicV
  • 650
  • 7
  • 16