system.net.http not found in Xamarin Forms - xamarin

In my xamarin application I need to implement the web-service using HttpClient.
So I create the test project & test the web-service - No problem here.
My code:
public async Task<string> registrationRequest()
{
HttpClient client = new HttpClient();
string response = await client.GetStringAsync(registrationURL);
return response;
}
My problem:
After test the testing application I use the same code in my live project
system.net.http not found error.
HttpClient - not resolved.
In my test project I am not install any Nuget packages.
I just use the
using System.Net.Http;
But in my live project this import return error.
Anyone help me out..

You need to install the Microsoft.Net.Http nuget package in your PCL and platform-specific projects.

Related

How to bind service from Java library in Xamarin Android Project

I have service class implementation in Example.aar
public class DemoService : Service{}
I have created a Native binding library project.
When i tried to bind this service in Xamarin.Android project. bindService() is returning false.
var Result =context.getApplicationContext.bindService(new Intent(context,typeof(DemoService)), new ServiceConnection(),Bind.AutoCreate); //Result is false.
How to bind a service created in 3rd party library project in Xamarin Android project.??
You can refer to this article Binding a Java Library.
In the Microsoft doc, first you can get the class from the .aar file.
var ClassName = new Com.MyCompany.MyProject.MyclassName();
Then use the bindService or other methods to use the Class.
var Result =context.getApplicationContext.bindService(new Intent(context,typeof(ClassName)), new ServiceConnection(),Bind.AutoCreate);

How to use SignalR with aspnetcore 2.0.0 preview

I am trying to use SignalR with aspnetcore 2.0.0 preview. The preview version is supposed to have support for signalR as per the release notes and I can add references to the "Microsoft.aspnetcoreSignalR" package.
However, I am unable to find the MapSignalR extension method or something like useSignalR() on the IApplicationBuilder.
Googling around for this gives lots of examples of the UseAppBuilder extension method. However, I still get stuck at MapSignalR with the below code:
using Microsoft.AspNetCore.Builder;
using Microsoft.Owin.Mapping;
using Microsoft.Owin.Helpers;
using Microsoft.Owin.Extensions;
using Microsoft.Owin.Builder;
using Owin;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace MyNamespace
{
using AppFunc = Func<IDictionary<string, object>, Task>;
public static class SignalRExtensions
{
public static IApplicationBuilder UseAppBuilder(this IApplicationBuilder app, Action<IAppBuilder> configure)
{
app.UseOwin(addToPipeline =>
{
addToPipeline(next =>
{
var appBuilder = new AppBuilder();
appBuilder.Properties["builder.DefaultApp"] = next;
configure(appBuilder);
return appBuilder.Build<AppFunc>();
});
});
return app;
}
public static void UseSignalR2(this IApplicationBuilder app)
{
app.UseAppBuilder(appBuilder => appBuilder.MapSignalR());
}
}
}
This is unable to find the MapSignalR extension method. Any thoughts on if I can find this some nuget package that will work with aspnetcore? Or is there a different mechanism to initialize signalR with aspnetcore 2.0.0 preview?
I am using visual studio 2017 preview 15.3 with .Net core 2.0 preview installed.
I basically hada the wrong set of nugget references. If I only have aspnetcore 2.0.0 preview 1 and the corresponding Microsoft.aspnetcore.signalR 1.0.0 preview package, there is already a UseSignalR() method available as an extension method for IApplicationBuilder and we don't need any of the above code.

HttpClient + gzip at PCL causes method not found: 'System.Net.Http.HttpClientHandler.ser_AutomaticDecompression'

I'm adding gzip compression support to an Xamarin.Android app where HttpClient reside in Portable Class Library (Profile49).
Every time I'm trying to make a HttpClient call I'm getting the following error:
Method not found:
'System.Net.Http.HttpClientHandler.ser_AutomaticDecompression
the code is the following:
var handler = new HttpClientHandler();
handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
using (var client = new HttpClient(handler))
...
If I'm not mistaken you'll need to add a reference to the httpclient package in the project you consume your PCL in. A reference within your PCL is not going to be enough, your other projects need to reference the httpclient BCL nuget package as well.

Ninject dependency for Ninject.Web.WebApi.OwinHost v3.2.x

I'm attempting to ninject ApiControllers in an Azure worker role. I've previously accomplished ninjection with the same controllers in an asp.net mvc 5 / web api 2 project in the same solution. Another non ninjected test controller in this WorkerRole project work and serve expected results.
While attempting to add the NuGet package Ninject.Web.WebApi.OwinHost (version 3.2.x) it looks like that package is ?erroneously? requiring a Ninject version ≥ 3.0.0.0 && < 3.1.0.0
I've tried
Install-Package Ninject.Web.WebApi.OwinHost -ProjectName WorkerRole1
Install-Package Ninject.Web.WebApi.OwinHost -Pre -ProjectName WorkerRole1
Install-Package Ninject.Web.WebApi.OwinHost -Version 3.2.0 -ProjectName WorkerRole1
Install-Package Ninject.Web.WebApi.OwinHost -Version 3.2.1-unstable-002 -ProjectName WorkerRole1
I was able to successfully add Ninject.Web.WebApi.SelfHost, however I believe I need the owin flavor and SelfHost didn't appear to install NinjectWebCommon.cs or equivalent. I don't know where to wire up ninject to the owin self host in the worker role once I am successful at including the OwinHost. I'm assuming it will be scaffold-ed in.
Result of attempting to install Ninject.Web.WebApi.OwinHost
PM> Install-Package Ninject.Web.WebApi.OwinHost -Pre -ProjectName WorkerRole1
Attempting to resolve dependency 'Ninject.Web.WebApi (≥ 3.2.0-unstable)'.
Attempting to resolve dependency 'Ninject (≥ 3.2.0.0 && < 3.3.0.0)'.
Attempting to resolve dependency 'Ninject.Web.Common (≥ 3.2.0.0 && < 3.3.0.0)'.
Attempting to resolve dependency 'Microsoft.AspNet.WebApi (≥ 5.0 && < 6.0)'.
Attempting to resolve dependency 'Microsoft.AspNet.WebApi.WebHost (≥ 5.1.2 && < 5.2.0)'.
Attempting to resolve dependency 'Microsoft.AspNet.WebApi.Core (≥ 5.1.2 && < 5.2.0)'.
Attempting to resolve dependency 'Microsoft.AspNet.WebApi.Client (≥ 5.1.2)'.
Attempting to resolve dependency 'Newtonsoft.Json (≥ 4.5.11)'.
Attempting to resolve dependency 'Ninject.Web.Common.OwinHost (≥ 3.0 && < 4.0)'.
Attempting to resolve dependency 'Ninject.Extensions.ContextPreservation (≥ 3.0 && < 4.0)'.
Attempting to resolve dependency 'Ninject (≥ 3.0.0.0 && < 3.1.0.0)'.
Install-Package : Updating 'Ninject 3.2.2.0' to 'Ninject 3.0.1.10' failed. Unable to find versions of 'Ninject.Web.WebApi, Ninject.Web.Common' that are compatible with 'Ninject 3.0.1.10'.
At line:1 char:1
+ Install-Package Ninject.Web.WebApi.OwinHost -Pre -ProjectName WorkerRole1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Install-Package], InvalidOperationException
+ FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PowerShell.Commands.InstallPackageCommand
PM>
Probably unrelated however in checking versions of installed package in the WorkerRole I note that a number of packages are IIS specific. Are these needed in an Azure WorkerRole Owin self host? And specifically for Microsoft.AspNet.WebApi.SelfHost I see the following notation yet I do have Microsoft.AspNet.WebApi.OwinSelfHost installed.
This is a legacy package for hosting ASP.NET Web API within your own
process (outside of IIS). Please use the
Microsoft.AspNet.WebApi.OwinSelfHost package for new projects.
Installed packages in WorkerRole
PM> Get-Package -ProjectName WorkerRole1
Id Version Description/Release Notes
-- ------- -------------------------
EntityFramework 6.1.0 Entity Framework is Microsoft's recommended data access technology for new applications.
Microsoft.AspNet.Identity.Core 2.0.0 Core interfaces for ASP.NET Identity.
Microsoft.AspNet.Identity.E... 2.0.0 ASP.NET Identity providers that use Entity Framework.
Microsoft.AspNet.WebApi 5.0.0 This package contains everything you need to host ASP.NET Web API on IIS. ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of client...
Microsoft.AspNet.WebApi.Client 5.1.2 This package adds support for formatting and content negotiation to System.Net.Http. It includes support for JSON, XML, and form URL encoded data.
Microsoft.AspNet.WebApi.Core 5.1.2 This package contains the core runtime assemblies for ASP.NET Web API. This package is used by hosts of the ASP.NET Web API runtime. To host a Web API in IIS use the Microsoft.AspNe...
Microsoft.AspNet.WebApi.Owin 5.1.2 This package allows you to host ASP.NET Web API within an OWIN server and provides access to additional OWIN features.
Microsoft.AspNet.WebApi.Owi... 5.1.2 This package allows you to host ASP.NET Web API within your own process using the OWIN HttpListener server. ASP.NET Web API is a framework that makes it easy to build HTTP services ...
Microsoft.AspNet.WebApi.Sel... 5.1.2 This is a legacy package for hosting ASP.NET Web API within your own process (outside of IIS). Please use the Microsoft.AspNet.WebApi.OwinSelfHost package for new projects.
Microsoft.AspNet.WebApi.Web... 5.0.0 This package contains everything you need to host ASP.NET Web API on IIS. ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of client...
Microsoft.Data.Edm 5.6.1 Classes to represent, construct, parse, serialize and validate entity data models. Targets .NET 4.0, Silverlight 4.0, or .NET Portable Lib with support for .NET 4.0, SL 4.0, Win Pho...
Microsoft.Data.OData 5.6.1 Classes to serialize, deserialize and validate OData payloads. Enables construction of OData producers and consumers. Targets .NET 4.0, Silverlight 4.0 or .NET Portable Lib with sup...
Microsoft.Data.Services.Client 5.6.1 LINQ-enabled client API for issuing OData queries and consuming OData payloads. Supports OData v3. Targets .NET 4.0, Silverlight 4.0 or .NET Portable Lib with support for .NET 4.5, ...
Microsoft.Owin 2.1.0 Provides a set of helper types and abstractions for simplifying the creation of OWIN components.
Microsoft.Owin.Diagnostics 2.1.0 Provides middleware components to assist in developing OWIN-based applications.
Microsoft.Owin.Host.HttpLis... 2.1.0 OWIN server built on the .NET Framework's HttpListener class. Currently the default server used for self-hosting.
Microsoft.Owin.Hosting 2.1.0 Provides default infrastructure types for hosting and running OWIN-based applications.
Microsoft.Owin.SelfHost 2.1.0 Includes components needed to host an OWIN-based application in a custom process.
Microsoft.WindowsAzure.Conf... 2.0.3 Windows Azure Configuration Manager provides a unified API to load configuration settings regardless of where the application is hosted - whether on-premises or in a Cloud Service.
Newtonsoft.Json 6.0.2 Json.NET is a popular high-performance JSON framework for .NET
Ninject 3.2.2.0 Stop writing monolithic applications that make you feel like you have to move mountains to make the simplest of changes. Ninject helps you use the technique of dependency injection ...
Ninject.Web.Common 3.2.0.0 Bootstrapper for web projects
Ninject.Web.Common.Selfhost 3.2.0.0 Adds self hosting support to web common.
Ninject.Web.WebApi 3.2.0.0 Extension for Ninject providing integration with ASP.NET MVC WebAPI 2
Ninject.Web.WebApi.Selfhost 3.2.0.0 Extension for Ninject providing integration with ASP.NET MVC WebAPI 2 Selfhost
Owin 1.0 OWIN IAppBuilder startup interface
System.Spatial 5.6.1 Contains classes and methods that facilitate geography and geometry spatial operations. Targets .NET 4.0, Silverlight 4.0 or .NET Portable Lib with support for .NET 4.0, SL 4.0, Win...
WindowsAzure.Storage 3.1.0.1 This client library enables working with the Windows Azure storage services which include the blob service for storing binary and text data, the table service for storing structured...
PM>
I know this is late, but I ran into this same problem and found this question via a Google search. I eventually got around it with -DependencyVersion:
Install-Package Ninject.Web.WebApi.OwinHost -DependencyVersion Highest
If you don't want to update all of the dependent NuGet packages, I found that manually installing all of the Ninject-related dependencies for Ninject.Web.WebApi.OwinHost and then installing Ninject.Web.WebApi.OwinHost also got around the issue.
I was facing the same problem today and I solved it by the following
add the following class implementation to your project
public class NinjectDependencyScope : IDependencyScope
{
IResolutionRoot resolver;
public NinjectDependencyScope(IResolutionRoot resolver)
{
this.resolver = resolver;
}
public object GetService(Type serviceType)
{
if (resolver == null)
throw new ObjectDisposedException("this", "This scope has been disposed");
return resolver.TryGet(serviceType);
}
public System.Collections.Generic.IEnumerable<object> GetServices(Type serviceType)
{
if (resolver == null)
throw new ObjectDisposedException("this", "This scope has been disposed");
return resolver.GetAll(serviceType);
}
public void Dispose()
{
IDisposable disposable = resolver as IDisposable;
if (disposable != null)
disposable.Dispose();
resolver = null;
}
}
// This class is the resolver, but it is also the global scope
// so we derive from NinjectScope.
public class NinjectDependencyResolver : NinjectDependencyScope, IDependencyResolver
{
IKernel kernel;
public NinjectDependencyResolver(IKernel kernel)
: base(kernel)
{
this.kernel = kernel;
}
public IDependencyScope BeginScope()
{
return new NinjectDependencyScope(kernel.BeginBlock());
}
}
2- then in NinjectWebCommon
add the following line of code to this method CreateKernel():
GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);
so the complete implementation will be:
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
try
{
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
// Install our Ninject-based IDependencyResolver into the Web API config
GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);
RegisterServices(kernel);
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}
I tried this code my self and it works successfully

Error using Autofac with ASP.NET Web API RC and ASP.NET MVC3

I added ASP.NET Web API RC to my MVC3 project using NuGet:
Install-Package AspNetWebApi
Then I configured it. In Global.asax.cs:
// configure regular controllers
var configuration = GlobalConfiguration.Configuration;
var container = Bootstrapper.ConfigureContainer(configuration);
containterProvider = new ContainerProvider(container);
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
// Set the dependency resolver implementation for Web API.
var resolver = new AutofacWebApiDependencyResolver(container);
configuration.DependencyResolver = resolver;
And in Boostrapper.ConfigureContainer(...) I added:
// I register my types as InstancePerLifetimeScope()
// but I also tried .InstancePerHttpRequest().InstancePerApiRequest()
// to the same end
builder.RegisterType<SomeService>()
.AsImplementedInterfaces().InstancePerLifetimeScope();
// Register API controllers using assembly scanning.
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
builder.RegisterWebApiFilterProvider(config);
This is described here and here.
I also updated Autofac, Autofac.Web, Autofac.Mvc3 packages using NuGet and installed Autofac.WebApi package.
With this configuration I tried running my ApiController and got the following error:
No scope with a Tag matching 'httpRequest' is visible from the scope in which the instance was requested. This generally indicates that a component registered as per-HTTP request is being reqested by a SingleInstance() component (or a similar scenario.) Under the web integration always request dependencies from the DependencyResolver.Current or ILifetimeScopeProvider.RequestLifetime, never from the container itself.
Then as I read this comment from Alex Meyer-Gleaves:
I suspect you are using the Web API package with MVC3 and this is causing the problem. In the MVC3 integration the tag for the InstancePerHttpRequest lifetime scope was "httpRequest". It was in the MVC4 Beta and Web API Beta packages that I changed both InstancePerHttpRequest and InstancePerApiRequest to use the common tag "AutofacWebRequest". You can grab the MVC4 integration package from NuGet using Autofac.Mvc4.
source article with comment
So following the advice from Alex I got the package Autofac.Mvc4 but it works only with Mvc4 and my project wouldn't build. I then grabbed the source code of Autofac to build Autofac.Mvc4 against Mvc3:
hg clone https://code.google.com/p/autofac/ --branch "MVC4 beta" C:\my\path
After using this assembly as my reference ApiController started working but regular Controllers worked ok only for a single controller action call. When the view called Html.RenderAction(...) and when I refresh or navigate to another controller action it crashes with this error:
No scope with a Tag matching 'AutofacWebRequest' is visible from the scope in which the instance was requested. This generally indicates that a component registered as per-HTTP request is being reqested by a SingleInstance() component (or a similar scenario.) Under the web integration always request dependencies from the DependencyResolver.Current or ILifetimeScopeProvider.RequestLifetime, never from the container itself.
I thought building from the newest source for Autofac.Mvc4 version 2.6.2.859 against Mvc3 could help but I can't find the source for that. Or maybe there's something else wrong in here?
I found the problem. I also used Autofac.Integration.Web to inject dependencies into custom Membership and Role providers. But in WebLiftime.cs there was this line:
public static readonly object Request = "httpRequest";
Once I changed it to:
public static readonly object Request = "AutofacWebRequest";
and used the built assembly everything works fine and I get no errors :)
I believe this constant value should the same as in all projects Autofac.Integration.Web, Autofac.Integration.Mvc and 'Autofac.Integration.WebApi for Mvc4 so this supposedly is a bug.

Resources