Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Feb 14, 2012
2 parents 2bd28a7 + 9191aaf commit 971806c
Show file tree
Hide file tree
Showing 35 changed files with 660 additions and 1,228 deletions.
2 changes: 1 addition & 1 deletion NuGet/ServiceStack.Common/servicestack.common.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<metadata xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<id>ServiceStack.Common</id>
<version>3.4.3</version>
<version>3.4.8</version>
<authors>Demis Bellot</authors>
<owners>Demis Bellot</owners>
<summary>Opensource .NET and Mono REST Web Services framework</summary>
Expand Down
2 changes: 1 addition & 1 deletion NuGet/ServiceStack/servicestack.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<metadata xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<id>ServiceStack</id>
<version>3.4.3</version>
<version>3.4.8</version>
<authors>Demis Bellot</authors>
<owners>Demis Bellot</owners>
<summary>Opensource .NET and Mono REST Web Services framework</summary>
Expand Down
102 changes: 87 additions & 15 deletions src/ServiceStack.Common/ServiceClient.Web/AsyncServiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ public class AsyncServiceClient

public ICredentials Credentials { get; set; }

public bool StoreCookies;
public bool StoreCookies { get; set; }

public CookieContainer CookieContainer;
public CookieContainer CookieContainer { get; set; }

public string BaseUri { get; set; }

internal class RequestState<TResponse> : IDisposable
{
Expand Down Expand Up @@ -69,12 +71,38 @@ public RequestState()

public Action<TResponse, Exception> OnError;

#if SILVERLIGHT
public bool HandleCallbackOnUIThread { get; set; }
#endif

public void HandleSuccess(TResponse response)
{
if (this.OnSuccess == null)
return;

#if SILVERLIGHT
if (this.HandleCallbackOnUIThread)
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => this.OnSuccess(response));
else
this.OnSuccess(response);
#else
this.OnSuccess(response);
#endif
}

public void HandleError(TResponse response, Exception ex)
{
if (OnError != null)
{
OnError(response, ex);
}
if (this.OnError == null)
return;

#if SILVERLIGHT
if (this.HandleCallbackOnUIThread)
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => this.OnError(response, ex));
else
this.OnError(response, ex);
#else
OnError(response, ex);
#endif
}

public void StartTimer(TimeSpan timeOut)
Expand Down Expand Up @@ -122,6 +150,14 @@ public void SetCredentials(string userName, string password)

public StreamDeserializerDelegate StreamDeserializer { get; set; }

#if SILVERLIGHT
public bool HandleCallbackOnUIThread { get; set; }

public bool UseBrowserHttpHandling { get; set; }

public bool ShareCookiesWithBrowser { get; set; }
#endif

public void SendAsync<TResponse>(string httpMethod, string absoluteUrl, object request,
Action<TResponse> onSuccess, Action<TResponse, Exception> onError)
{
Expand All @@ -145,12 +181,34 @@ private RequestState<TResponse> SendWebRequest<TResponse>(string httpMethod, str
}
}

#if SILVERLIGHT

var creator = this.UseBrowserHttpHandling
? System.Net.Browser.WebRequestCreator.BrowserHttp
: System.Net.Browser.WebRequestCreator.ClientHttp;

var webRequest = (HttpWebRequest) creator.Create(new Uri(requestUri));

if (StoreCookies && !UseBrowserHttpHandling)
{
if (ShareCookiesWithBrowser)
{
if (CookieContainer == null)
CookieContainer = new CookieContainer();
CookieContainer.SetCookies(new Uri(BaseUri), System.Windows.Browser.HtmlPage.Document.Cookies);
}

webRequest.CookieContainer = CookieContainer;
}

#else
var webRequest = (HttpWebRequest)WebRequest.Create(requestUri);

if (StoreCookies)
{
webRequest.CookieContainer = CookieContainer;
}
#endif

var requestState = new RequestState<TResponse>
{
Expand All @@ -160,6 +218,9 @@ private RequestState<TResponse> SendWebRequest<TResponse>(string httpMethod, str
Request = request,
OnSuccess = onSuccess,
OnError = onError,
#if SILVERLIGHT
HandleCallbackOnUIThread = HandleCallbackOnUIThread,
#endif
};
requestState.StartTimer(this.Timeout.GetValueOrDefault(DefaultTimeout));

Expand All @@ -173,25 +234,25 @@ private void SendWebRequestAsync<TResponse>(string httpMethod, object request,
{
var httpGetOrDelete = (httpMethod == "GET" || httpMethod == "DELETE");
webRequest.Accept = string.Format("{0}, */*", ContentType);

#if !SILVERLIGHT
webRequest.Method = httpMethod;
webRequest.Method = httpMethod;
#else
//Methods others than GET and POST are only supported by Client request creator, see
//http://msdn.microsoft.com/en-us/library/cc838250(v=vs.95).aspx

if (webRequest.CreatorInstance.GetType().Name == "BrowserHttpWebRequestCreator" //internal class :(
&& httpMethod != "GET" && httpMethod != "POST")

if (this.UseBrowserHttpHandling && httpMethod != "GET" && httpMethod != "POST")
{
webRequest.Method = "POST";
webRequest.Headers[HttpHeaders.XHttpMethodOverride] = httpMethod;
}else
}
else
{
webRequest.Method = httpMethod;
}
#endif


if (this.Credentials != null)
if (this.Credentials != null)
{
webRequest.Credentials = this.Credentials;
}
Expand Down Expand Up @@ -301,10 +362,20 @@ private void ReadCallBack<T>(IAsyncResult asyncResult)
response = (T)this.StreamDeserializer(typeof(T), reader);
}

if (requestState.OnSuccess != null)
#if SILVERLIGHT
if (this.StoreCookies && this.ShareCookiesWithBrowser && !this.UseBrowserHttpHandling)
{
requestState.OnSuccess(response);
// browser cookies must be set on the ui thread
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(
() =>
{
var cookieHeader = this.CookieContainer.GetCookieHeader(new Uri(BaseUri));
System.Windows.Browser.HtmlPage.Document.Cookies = cookieHeader;
});
}
#endif

requestState.HandleSuccess(response);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -381,4 +452,5 @@ private void HandleResponseError<TResponse>(Exception exception, RequestState<TR

public void Dispose() { }
}

}
Loading

0 comments on commit 971806c

Please sign in to comment.