简体   繁体   中英

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

图片2

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM