Is it possible to dynamically change the namespace in ServiceContract at run time?

ex:

[ServiceContract(Namespace = "@ services.url@ /", Name = "FedExContract")] 

I need to change the value of "@ services.url @" at runtime.

+6
source share
2 answers

You can’t think of anything other than using conditional compilation symbols, i.e.

 #if Debug [ServiceContract(Namespace = "@ AA.BBB@ /", Name = "FedExContract")] #endif 

Namespaces must be static, as you may need to recreate the client proxy if you change the namespace of the contract.

0
source

You can change it at runtime. But that would be a lot of work, and it should have happened before the service began. After starting the service, you cannot change the contract information.

Personally, I do not like the start of the service, depending on the database. If something breaks, the service never appears, and troubleshooting can be a difficult process for IT. If I needed to run the execution route, I would save the namespace value in my configuration file. It just keeps it simple and still allows you to replace the token after deployment, like what you describe in your question.

From the comments, you indicate the build process. Here's how we handle this: make the namespace a constant string. For example:

 [DataContract(Namespace=Constants.CURRENT_NAMESPACE] public class MyClass { } 

Now in a separate file, declare:

 // in a separate file define: public static class Constants { public const string CURRENT_NAMESPACE = "url://Services"; }; 

If you want to create for another environment, replace the Constants file for the new definition:

 //As a part of your build process switch the Constants file: public static class Constants { public const string CURRENT_NAMESPACE = "url://Eclipse/Services"; }; 

It is easy to manage, you always know what namespace is used ... and the service always starts. It works?

0
source

Source: https://habr.com/ru/post/915692/


All Articles