Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
972 views
in Technique[技术] by (71.8m points)

c# - Error when compiling - 'Cannot implicitly convert type 'string' to 'System.Uri'

HI I am getting this error when compiling in the beginning at SMS.Rest.Client. We are trying to use the Celltrust SMS server to facilitate messaging in our app. -------------------------END____________-

using Lda.Sms.Client.Events;
using Lda.Sms.Models;
using Caliburn.Micro;
using Newtonsoft.Json;
using RestSharp;
using RestSharp.Deserializers;
using RestSharp.Serializers;
using System;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
                                       ------------------------------
namespace Lda.Sms.Client.Services {

    public class SmsRestClient : ISmsRestClient, IHandle<SuccessfulLoginEvent> {
        private RestClient client = new RestClient();
        string userId = null;

        public SmsRestClient(IEventAggregator eventAggregator) {
            eventAggregator.Subscribe(this);
            var address = ConfigurationManager.AppSettings["serverAddress"];
            client.BaseUrl = address + "api";
            client.AddHandler("application/json", new JsonNetSerializer());
        }

        public void Handle(SuccessfulLoginEvent message) {
            if (client.CookieContainer == null) {
                client.CookieContainer = new System.Net.CookieContainer();
            }
            client.CookieContainer.Add(message.AuthenticationCookie);
            userId = message.UserId;
        }
                              ---------------------------------------------
        #region Asynchronous Api

        public async Task<Settings> GetSettingsAsync() {
            return (await client.ExecuteTaskAsync<Settings>(CreateGetSettingsRequest())).Data;
        }

        public async Task<User> GetUserAsync() {
            return (await client.ExecuteTaskAsync<User>(CreateGetUserRequest())).Data;
        }

        public async Task UpdateSettingsAsync(Settings settings) {
            var response = await client.ExecuteTaskAsync(CreateUpdateSettingsRequest(settings));
            return;
        }

        public async Task SendMessageAsync(int contactId, string body) {
            var response = await client.ExecuteTaskAsync(CreateSendMessageRequest(contactId, body));
            return;
        }

        #endregion Asynchronous Api

        #region Synchronous Api

        public Settings GetSettings() {
            return client.Execute<Settings>(CreateGetSettingsRequest()).Data;
        }

        public User GetUser() {
            return client.Execute<User>(CreateGetUserRequest()).Data;
        }

        public void SendMessage(int contactId, string body) {
            client.Execute(CreateSendMessageRequest(contactId, body));
        }

        public void UpdateSettings(Settings settings) {
            client.Execute(CreateUpdateSettingsRequest(settings));
        }

        #endregion Synchronous Api

        #region Rest Requests

        private RestRequest CreateGetSettingsRequest() {
            return new RestRequest {
                Resource = "Settings",
                Method = Method.GET
            };
        }

        private RestRequest CreateGetUserRequest() {
            var request = new RestRequest {
                Resource = "User/{userId}",
                Method = Method.GET
            };
            request.AddUrlSegment("userId", userId);
            return request;
        }

        private RestRequest CreateUpdateSettingsRequest(Settings settings) {
            var request = new RestRequest {
                JsonSerializer = new JsonNetSerializer(),
                Resource = "Settings",
                Method = Method.POST,
                RequestFormat = DataFormat.Json
            };
            request.AddBody(settings);
            return request;
        }

        private RestRequest CreateSendMessageRequest(int contactId, string body) {
            var request = new RestRequest {
                Resource = "Sms",
                Method = Method.POST,
            };
            request.AddParameter("contactId", contactId, ParameterType.QueryString);
            request.AddParameter("body", body, ParameterType.QueryString);
            return request;
        }

        #endregion Rest Requests
    }

    internal class JsonNetSerializer : IDeserializer, ISerializer {
        private Newtonsoft.Json.JsonSerializer _serializer;

        public JsonNetSerializer() {
            ContentType = "application/json";
            _serializer = new Newtonsoft.Json.JsonSerializer {
                MissingMemberHandling = MissingMemberHandling.Ignore,
                NullValueHandling = NullValueHandling.Include,
                DefaultValueHandling = DefaultValueHandling.Include,
                PreserveReferencesHandling = PreserveReferencesHandling.Objects,
            };
        }
            }

// adding more details here // adding more and more and more details more and details

//  Asher barah Sasone viSimcha sasone vi Simchas Chason Vi Kalah
// mnnm
        public string ContentType { get; set; }

        public string DateFormat { get; set; }

        public string Namespace { get; set; }

        public string RootElement { get; set; }

        public T Deserialize<T>(IRestResponse response) {
            using (var textReader = new StreamReader(new MemoryStream(response.RawBytes)))
            using (var jsonTextReader = new JsonTextReader(textReader))
                return _serializer.Deserialize<T>(jsonTextReader);
        }

// adding more details here // adding more and more and more details more and details

//  Asher barah Sasone viSimcha sasone vi Simchas Chason Vi Kalah
// mnnm
// details detail;s and detail;s 
        public string Serialize(object obj) {
            using (var textWriter = new StringWriter())
            using (var jsonTextWriter = new JsonTextWriter(textWriter)) {
                jsonTextWriter.Formatting = Formatting.Indented;
                jsonTextWriter.QuoteChar = '"';
                _serializer.Serialize(jsonTextWriter, obj);
                return textWriter.ToString();
            }
        }
    }
}

lplp this code is not successfully building... *** this issue is that this code is not building. *****

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You know you can do this right : ?

Uri uri = new Uri(whateverstring);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...