Nikita
Nikita

Reputation: 1082

Why can not use static readonly object property?

This is constants class:

 public static class Constants
    {
        public const string USER_ID = "conduent";
        public const string PASSWORD = "593becd1-02f6-46f0-bf34-25b393ad041b";
        public static readonly Uri BASE_URI = new Uri("https://staging.test-476b.com");
        public static readonly Uri GET_TOKEN_URI = new Uri("api/session");
        public static readonly Uri SEND_CASE_URI = new Uri("api/referral_request");
    }

And this is usage

public class DanestreetHttp
    {

        private AuthToken authToken = null;

        private readonly HttpClient httpClient = new HttpClient()
        {
            BaseAddress = Constants.BASE_URI
        };
}

On the screen shot you can see the error, that was disappeared after I have changed BaseAddress = Constants.BASE_URI to BaseAddress = new System.Uri("https://staging.test-476b.com"). What is wrong with static readonly initialiazation?

Screen

Image 2

PS. My current solution: BaseAddress = new Uri(Constants.BaseAddress)

Upvotes: 0

Views: 405

Answers (1)

vc 74
vc 74

Reputation: 38179

The problem is that 2 or the URIs are invalid in Constants, preventing this class to initialize property. It should work if you replace

public static readonly Uri GET_TOKEN_URI = new Uri("api/session");
public static readonly Uri SEND_CASE_URI = new Uri("api/referral_request");

with

public static readonly Uri GET_TOKEN_URI = new Uri("http://api/session");
public static readonly Uri SEND_CASE_URI = new Uri("http://api/referral_request");

(or https)

Fiddle

Upvotes: 1

Related Questions