I'm trying to get Ninject to work with a project that is using self a hosted WebApi connection.
I've installed the nuget package Microsoft ASP.NET Web API 2.2 Self Host (Microsoft.AspNet.WebApi.SelfHost) along with Ninject.Web.Common.Selfhost.
The console app seems to load up correctly, but I only seem to get a connection error when hitting my URL (http://localhost:8081/api/hello). I have a similar test app that is not using Ninject that works correctly.
I'm trying to follow this post: https://github.com/ninject/Ninject.Web.Common/wiki/Setting-up-a-self-hosted-web-application
Am I missing something to get this going? I originally tried using the package Microsoft.AspNet.WebApi.OwinSelfHost but could not get it functioning because of various errors. If this is what I need to be using, I can revisit this.
public class HelloController : ApiController
{
public string Get()
{
return "Hello, world!";
}
}
class Program
{
static void Main(string[] args)
{
var webApiConfiguration = new HttpSelfHostConfiguration("http://localhost:8081");
webApiConfiguration.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional });
var mSelfHost = new NinjectSelfHostBootstrapper(CreateKernel, webApiConfiguration);
mSelfHost.Start();
Console.ReadLine();
}
private static IKernel CreateKernel()
{
var mKernel = new StandardKernel();
mKernel.Load(Assembly.GetExecutingAssembly());
return mKernel;
}
}
Here are the packages I have installed:
<packages>
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.2" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.2" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.SelfHost" version="5.2.2" targetFramework="net45" />
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net45" />
<package id="Ninject" version="3.2.2.0" targetFramework="net45" />
<package id="Ninject.Web.Common" version="3.2.3.0" targetFramework="net45" />
<package id="Ninject.Web.Common.Selfhost" version="3.2.3.0" targetFramework="net45" />
</packages>
I was able to solve this by adding Ninject.Web.WebApi.SelfHost first. This loaded the appropriate WebApi packages automatically. Before I was loading the WebApi packages myself and then adding Ninject.
Related
Does Xamarin support multiple levels of project reference nesting, where the referenced project includes Android components intended for the AndroidManifest.xml?
For example: AndoidXamarin App > ClassLibrary1 > (which declares a Service) ClassLibrary2 (which also declares a Service):
// ClassLibrary1:
// Referenced by the XamarinApp
// References ClassLibrary2
namespace ClassLibrary1
{
[Service(Exported = true, Name = "com.company.test." + nameof(ClassLib1Service))]
public class ClassLib1Service : Service
{
public override IBinder OnBind(Intent intent) => throw new NotImplementedException();
}
}
// ClassLibrary2
// Referenced only by ClassLibrary1
namespace ClassLibrary2
{
[Service(Exported = true, Name = "com.company.test." + nameof(ClassLib2Service))]
public class ClassLib2Service : Service
{
public override IBinder OnBind(Intent intent) => throw new NotImplementedException();
}
}
// The generated `AndroidManifest.xml` only includes the Service defined in ClassLibrary1.
<application android:allowBackup="true" android:icon="#mipmap/ic_launcher" android:label="#string/app_name" android:roundIcon="#mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="#style/AppTheme" android:name="android.app.Application">
<activity android:label="#string/app_name" android:theme="#style/AppTheme" android:name="crc64a7a6b04b89628087.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:exported="true" android:name="com.company.test.ClassLib1Service" />
<provider android:name="mono.MonoRuntimeProvider" android:exported="false" android:initOrder="1999999999" android:authorities="com.company.test.mono.MonoRuntimeProvider.__mono_init__" />
</application>
I have added a Web API feature to an ASP.NET MVC app and want to use attribute routing. When running the site in Visual Studio 2015, I can enter URLs in the browser address bar and get the methods on my controller to work. Once I move the site to IIS, all I get is 404 responses. I have tried many code changes with no luck.
The BuilderApiController file is in a WebAPI/v1/Controllers folder. Will attributes on the controller be found here?
namespace Hds.Edsi.WebAPI.V1.Controllers
{
[System.Web.Http.RoutePrefix("api/v1/builder")]
public class BuilderApiController : BaseApiController
{
[System.Web.Http.Route("GetExternalOrganizationID/{envisionOrgID}")]
[System.Web.Http.HttpGet]
public HttpResponseMessage GetExternalOrganizationID(string envisionOrgID)
{
WebApiConfig.cs file. Standard stuff. If I am using attribute routing, do I need the config.Routes.MapHttpRoute part? I have tried it with and without.
If I need it, what is the purpose of config.MapHttpAttributeRoutes()? When I look at GlobalConfiguration,Configuration._routes after Register is called, none of the attribute routing defined routes are there. Should they be?
namespace Hds.Edsi.WebAPI
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API routes
config.MapHttpAttributeRoutes();
/*
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/v1/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
*/
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new StringEnumConverter());
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
Components.Automapper.CreateMaps();
// Requrired for managing AppDomain teardown - IRegisteredObject
MonitorManager.RegisterInstance();
}
IIS Express:
http://localhost:8181/api/v1/builder/GetExternalOrganizationID/123.123.123.123.1234 returns a JSON object as I would expect.
IIS:
http://my server/api/v1/builder/GetExternalOrganizationID/123.123.123.123.1234 returns a 404
I am guessing that I am missing something simple here or don't understand how attribute routing works.
Added
Here is the system.webServer section from web.config:
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true">
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" />
<add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" />
</modules>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*" verb="*" type="System.Web.Handlers.TransferRequestHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
</handlers>
</system.webServer>
As per as i know, if it's not working properly on your IIS, then that is an IIS error, not an ASP.NET error so this doesn’t actually come from ASP.NET’s routing engine but from IIS’s handling of expressionless URLs.
You can try with adding runAllManagedModulesForAllRequests to your web.config
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="ScriptCompressionModule" type="Westwind.Web.ScriptCompressionModule,Westwind.Web" />
</modules>
</system.webServer>
In detail you can refer Rick Strahl's Web Log ASP.NET Routing not working on IIS 7.0
Hope this helps!
Hoping someone can provide me with some guidance on an issue I'm facing using MEF with my VS2015 extension. I'm attempting to extend the IntelliSense capabilities of VS and followed this tutorial as a basis: https://msdn.microsoft.com/en-us/library/ee372314.aspx
Here is my ICompletionSourceProvider:
[Export(typeof(ICompletionSourceProvider))]
[Name("Test Statement Completion Provider")]
[Order(Before = "default")]
[ContentType("text")]
internal class MyCompletionSourceProvider : ICompletionSourceProvider
{
[Import]
internal ITextStructureNavigatorSelectorService NavigatorService { get; set; }
[Import]
internal IServiceProvider ServiceProvider { get; set; }
public ICompletionSource TryCreateCompletionSource(ITextBuffer textBuffer)
{
return new MyCompletionSource(this, textBuffer);
}
}
The strange thing here is that this works fine on my machine (where this code was initially developed on), but fails to work on every other machine I've tried (both in Debug and Release). Breakpoints placed within TryCreateCompletionSource are never hit. I suspected this MEF component was never being exported, so I downloaded the mefx tool as recommend on MSDN and executed it on my generated DLL and received the following:
[Primary Rejection]
[Export] MyVSIX.Source.MyCompletionSourceProvider (ContractName="Microsoft.VisualStudio.Language.Intellisense.ICompletionSourceProvider")
[Import] MyVSIX.Source.MyCompletionSourceProvider.NavigatorService (ContractName="Microsoft.VisualStudio.Text.Operations.ITextStructureNavigatorSelectorService")
[Exception] System.ComponentModel.Composition.ImportCardinalityMismatchException: No exports were found that match the constraint:
ContractName Microsoft.VisualStudio.Text.Operations.ITextStructureNavigatorSelectorService
RequiredTypeIdentity Microsoft.VisualStudio.Text.Operations.ITextStructureNavigatorSelectorService
at System.ComponentModel.Composition.Hosting.ExportProvider.GetExports(ImportDefinition definition, AtomicComposition atomicComposition)
at Microsoft.ComponentModel.Composition.Diagnostics.CompositionInfo.AnalyzeImportDefinition(ExportProvider host, IEnumerable`1 availableParts, ImportDefinition id)
[Import] MyVSIX.Source.MyCompletionSourceProvider.ServiceProvider (ContractName="System.IServiceProvider")
[Exception] System.ComponentModel.Composition.ImportCardinalityMismatchException: No exports were found that match the constraint:
ContractName System.IServiceProvider
RequiredTypeIdentity System.IServiceProvider
at System.ComponentModel.Composition.Hosting.ExportProvider.GetExports(ImportDefinition definition, AtomicComposition atomicComposition)
at Microsoft.ComponentModel.Composition.Diagnostics.CompositionInfo.AnalyzeImportDefinition(ExportProvider host, IEnumerable`1 availableParts, ImportDefinition id)
If I remove the [Import] statements these are all resolved and things begin working again. From the tutorial though, this seems as though it should just work. This also doesn't explain why it works 100% of the time on my machine and never works on any other machines.
This is my .vsixmanifest file:
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="MyVSIX.Company.39048217-955a-4402-84e9-8a24f0730e72" Version="1.0" Language="en-US" Publisher="MyVSIX" />
<DisplayName>MyVSIX</DisplayName>
<Description xml:space="preserve"></Description>
<Icon>Resources\MyVSIX.ico</Icon>
</Metadata>
<Installation>
<InstallationTarget Id="Microsoft.VisualStudio.Community" Version="[14.0]" />
</Installation>
<Dependencies>
<Dependency Id="Microsoft.Framework.NDP" DisplayName="Microsoft .NET Framework" d:Source="Manual" Version="[4.5,)" />
<Dependency Id="Microsoft.VisualStudio.MPF.14.0" DisplayName="Visual Studio MPF 14.0" d:Source="Installed" Version="[14.0]" />
</Dependencies>
<Assets>
<Asset Type="Microsoft.VisualStudio.VsPackage" d:Source="Project" d:ProjectName="%CurrentProject%" Path="|%CurrentProject%;PkgdefProjectOutputGroup|" />
<Asset Type="Microsoft.VisualStudio.MefComponent" d:Source="Project" d:ProjectName="%CurrentProject%" Path="|%CurrentProject%|" />
<Asset Type="Microsoft.VisualStudio.ItemTemplate" Path="Output\ItemTemplates" />
<Asset Type="Microsoft.VisualStudio.ProjectTemplate" Path="Output\ProjectTemplates" />
</Assets>
</PackageManifest>
Figured out why it wasn't working on other machines, but I still cannot understand why it was ever working on my machine. I changed this:
[Import]
internal IServiceProvider ServiceProvider { get; set; }
to this:
[Import(typeof(SVsServiceProvider)]
internal IServiceProvider ServiceProvider { get; set; }
Hope this helps someone in the future - I spent 2 days figuring this out!
I have a singleton service on remote server, this has a method who returns new objects to clients:
public class MySingleton : MarshalByRefObject
{
public override object InitializeLifetimeService()
{
return null;
}
public MarshalByRefObject GetService()
{
return new Model();
}
}
public class Model : MarshalByRefObject
{
}
I don't want that Model instances live forever on server, so I just wanted to use the normal sponsorship procedure, on client side I create a sponsor for my Model, and I attach the remote lease to this sponsor:
var sponsor = new ClientSponsor();
_service = _mySingleton.GetService();
var success = sponsor.Register(_service);
Well, this does not work. The remote object Model, dies after a while.
Do you confirm this behavior ?
I guess it's because the lifetime manager on server doesn't have the opportunity to initialize the lease, because the object Model is instanced and returned directly.
I know this is an old post but during my search for an similar issue I stumbled on this post.
Maybe it will depend on the SinkProvider configuration. Because the Renewal call on Client side from server require an deserialization. In the app.exe.config on server side you have to setup the serverProvider and also the clientProvider like this:
<channel ref="tcp" port="50220" useIpAddress="false">
<serverProviders>
<formatter ref="binary" typeFilterLevel="Full" />
</serverProviders>
<clientProviders>
<formatter ref="binary" />
</clientProviders>
</channel>
<channel ref="http" port="50221" useIpAddress="false">
<serverProviders>
<formatter ref="soap" typeFilterLevel="Full" />
</serverProviders>
<clientProviders>
<formatter ref="soap" />
</clientProviders>
</channel>
On the client side use the following app.config.exe:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<channels>
<channel ref="tcp" port="0">
<serverProviders>
<formatter ref="binary" typeFilterLevel="Full"/>
</serverProviders>
</channel>
</channels>
</application>
</system.runtime.remoting>
</configuration>
I have version 1.6 of the MvcMiniProfiler referenced (via Nuget) and have set everything up as described on the project homepage at http://code.google.com/p/mvc-mini-profiler/.
I have the following code in the Web.config:
<system.data>
<DbProviderFactories>
<remove invariant="MvcMiniProfiler.Data.ProfiledDbProvider" />
<add name="MvcMiniProfiler.Data.ProfiledDbProvider" invariant="MvcMiniProfiler.Data.ProfiledDbProvider" description="MvcMiniProfiler.Data.ProfiledDbProvider" type="MvcMiniProfiler.Data.ProfiledDbProviderFactory, MvcMiniProfiler, Version=1.6.0.0, Culture=neutral, PublicKeyToken=b44f9351044011a3" />
</DbProviderFactories>
</system.data>
(The project homepage has Version=1.5.0.0 - the NuGet package has since been updated)
I have the following code in the Global.asax (and connection string also defined in Web.config):
protected void Application_Start()
{
Log.Info("ReCoupon has started.");
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
var factory = new SqlConnectionFactory(ConfigurationManager.ConnectionStrings["ReCouponContext"].ConnectionString);
var profiled = new MvcMiniProfiler.Data.ProfiledDbConnectionFactory(factory);
Database.DefaultConnectionFactory = profiled;
Database.SetInitializer(new ReCouponContextInitializer());
}
The profiler works great except that I can't get it to profile SQL. I am using SQL Server 2008 Express. I've been following the related issues on the Google Code project homepage and am totally stuck.
This one had me stumped for a long time too. It appears that the connection string naming convention takes precedence over Database.DefaultConnectionFactory.
Could you try renaming the connection string in the web.config?
from
<connectionStrings>
<add name="ReCouponContext" connectionString="..." />
</connectionStrings>
to
<connectionStrings>
<add name="ReCoupon" connectionString="..." />
</connectionStrings>
and then change
var factory = new SqlConnectionFactory(ConfigurationManager.ConnectionStrings["ReCouponContext"].ConnectionString);
to
var factory = new SqlConnectionFactory(ConfigurationManager.ConnectionStrings["ReCoupon"].ConnectionString);