.NET Conditional Web Services - c#

My C# application has three testing stages: alpha, staging and production. Each stage for my application has a sibling web service. The APIs for all three web services are the same.
How could my application use the appropriate sibling web service based solely on alterations to the web.config file?
The goal being to have very little duplicate code, and for the source for my application to be the same across all stages.

If you use WCF, the endpoint can easily be defined entirely in the config file.

quick case for xml web services: define some appsettings and whenever you new a proxy, use the appsetting as the constructor arg.
but steven has a better suggestion for you, use WCF if you can.

Related

How to config WCF settings from 2 different web applications

I have a WCF project that send emails, log them, and know how to use templating.
There are 2 different web sites that have a service reference to this project, but they need also to provide a From address to the service which I don't want it to be send at each method call, I consider this to be a setting of the service behavior. What is the correct way to do it?
With referencing different project I can use the settings properties section in the web.config, but with WCF this solution won't work.
If I'm understanding correctly you just need common settings between multiple projects, right?
I would go with appsettings and Dependency Injection. It's origin is asp.net core but you could apply the principle anywhere.
For instance:
https://csharp.christiannagel.com/2016/08/16/diwithconfiguration/

use owin to self host web api

I'm self hosting web api using owin on client-server application based on MVC architecture. I see a lot of code examples that shows that the Startup class with the configuration, and the Program class with the Main method that start the owin self host "using (WebApp.Start(url: baseAddress))" - are at the same project. Should I desperate owin self host to one project so the WebApp.Start will be in one project, and web api with Startup claas to another one, with all the controllers and so?
The common answer is that it depends. Usually self host applications is very simple to provide some simple functionality. In that case since both parts are simple there is no need to separate them (host code and Asp.Net Mvc) and they can be placed in one project as an application layer. But if you are trying to add some complexity to host code (e.g. some warm up of Asp.Net Mvc app), then may be you should split your host code and Asp.Net Mvc app code in to two projects.
Found that it might be good idea inspired by separation of concerns: the self host should not know about the server and the opposite. Owin and web api have different job so they need to be separated.

Examples showing conversion of a WCF REST service to ASP.NET OWIN?

I've got a stand alone WCF service providing REST services. As time goes on, I'm feeling like MS is going to reduce support for WCF and REST services, and I've already had to code in a bunch of hacks to get somethings working like CORS. Additionally, implementing https is another hack since it requires using OS functionality. (i.e. you have to install the certificate using an external application, which doesnt' make the service very self contained)
In anycase, I'm considering either a move to ASP.NET REST API and OWIN, or Java/Spring. I wanted to first see what it would take to move to ASP.NET REST, since I figured that should take the smallest amount of work, but I haven't found any good examples.
There's a pretty straightforward example on the asp.net site.
http://www.asp.net/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api
setup a self-contained project i.e console app or windows service
initialise and setup you routes
create your Controller / API stubs
deploy
Regarding SSL, whether it's IIS or Apache you will have to configure it on the web server regardless.

Encapsulating multiple WCF services into a class library

I have one WCF service with multiple endpoints. Each endpoint has it's own configuration.
My problem is that I'm trying to figure out what will be the best.
1 - Add to my MVC application a service reference to each of the endpoints
2 - Create new DLL that will have the references to each of the endpoints and then add in my MVC application a reference only to this DLL.
I could really use your help to figure if there are any downsides to each of the approaches?
UPDATE
i forgot to mention that i have multiple MVC applications and each one uses only one or two of the WCF services.
to be more accurate, i now have 6 MVC applications and 7 WCF services. each MVC application uses only 2 WCF.
in the future the number of MVC apps and WCF will grow.
I would not use a server reference but just point svcutil to all three at the same time. It will generate one set of proxies and one configuration. It also allows you to share data contracts between the services.
Personally, I always put my web references and service references in a standalone assembly called SharedServices. That way; multiple assemblies can share the same references and datatypes can be shared among assemblies. Attaching a web reference to an assembly could lead to many projects being dependent on that assembly solely for the web service defintions.
You can write a service agent that is responsible for accessing the services, abstracting away this logic for your MVC application. The service agent would also be the place to implement other logic like caching, if you would ever need it. See http://tinyurl.com/cbcepgl, below under 'service agent' for some demo code.

Dynamic Webservice reference from Class Library used in Winforms app (c#)

Ok - pretty basic scenario, been there before, seemed all so simple - but can't recall enough to work out what's different about the setup at this particular existing codebase.
Winforms App calls Dll which calls Web Service. Reference in the Dll to the Web Service is dynamic. How do I get the URI for the Web Service into a Winforms app.config so I can easily change it for test, dev, live etc.
[Oh just to make it interesting, though I can't see it mattering, the proxy for the web service needs to NOT be regenerated as we have customised it...]
Set the URL directly in your code.
YourServiceProxy service = new YourServiceProxy();
service.Url = ConfigurationManager.AppSettings["YourURLKey"];
Can you configure the web service URI dynamically in code? That way you can easily modify the service to point to the desired location.
You can set the Url property of the webservice in code to point to the URI and use Proxy to set the proxy to your custom proxy.
What's wrong with just copying the URL from the app.config of the library into the app.config of the Windows Forms application?
Also, I'll suggest strongly that you do not modify generated code, ever. You can make many customizations of the proxy by using partial classes. See Ways to Customize your ASMX Client Proxy.

Categories