Autofac null service resolve exception - c#

I have a 3 project in my solution:
Portable Library (for ios, android and winphones), library project(for windows phone) and windows phone test project. My library project got reference to portable library project, and my windows phone test project got reference to portable and winphone library project. In my portable library project I got some service interface which I implement in library winphone project. In my portable project I want have some class that have fields with service that I implement in windows phone library and use this class in windows phone test project. To do this i try use Autofac library, to register types in my windows phone library( to do this I made class with static ctor) and try to resolve service in my portable library class which I register in my windows phone library, but I got null exception and I don't know how to fix that, and it is possible to do things which I actually trying to do ?
My class from portable library project, after call Resolve method I go exception.
public IDeviceInformation DeviceInformation { get; }
public Logger()
{
DeviceInformation = AutofacContainer.Instance.Resolve<IDeviceInformation>();
}
Class from library project
public class AutoFacRegister
{
static AutoFacRegister()
{
var builder = new ContainerBuilder();
builder.RegisterType<WindowsDeviceInformation>()
.As<IDeviceInformation>();
AutofacContainer.Instance = builder.Build();
}
}

I'd recommend against using a static constructor to create your container. I suspect that the constructor is never called since the AutoFacRegister class is not used anywhere and therefore AutofacContainer.Instance will always be null.
I would suggest to use modules to achieve your goal.

Related

Problem with platform specific code in .NET MAUI and Jetbrains Rider

i am having the following problem. i try to invoke platform specific code in the .NET MAUI from the microsoft tutorial.
https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/invoke-platform-code
The problem here is that it can be compiled and builded with Visual Studio, but with Rider from Jetbrains it cant be compiled.
i m getting the following error message:
DeviceOrientationService.cs(5, 38): [CS8795] Partial method 'DeviceOrientationService.GetOrientation()' must have an implementation part because it has accessibility modifiers.
Anyone got an idea what i am missing?
So the thing is you need to create an implementation for your abstract method on all platform classes for it to be able to build since you are targeting multiple platforms.
So just like you have an implementation in your Android and iOS platforms you need it on others as well.
There is another way as well which is you can create an abstract method with an implementation that already does something on other platforms so assume this method is only relevant on Android and iOS then you would do something like below in this class:
public partial class DeviceOrientationService
{
#if ANDROID || IOS
public partial DeviceOrientation GetOrientation();
#else
public partial DeviceOrientation GetOrientation()
{
return DeviceOrientation.Undefiend;
}
#endif
}
Also if you don't want to support Tizen, Windows or MacCatalyst you can just remove their references from the csproj file and then you can delete their platform folders and you won't need to do the above-mentioned things at all your app will only expect Android and iOS code for the above project.

Attribute.IsDefined is not available in portable class library

Why I do not have access to Attribute.IsDefined() in a portable class library? How could I use this method in PCL? In a class library the following code works perfect but in a pcl it says there is no method there.
if (Attribute.IsDefined(item, typeof(Title))) ...
Title is an attribute. Is it possible to solve this problem by adding a reference or something to the project?

Xamarin iOS Dependency services always return null object in iOS application

I use Xamarin Studio and work on iOS Dependency Services
Here I have tried to resolve the runtime issue in case of getting the object through dependency services.
We have tried the same in sample projects as below:
1) Created a PCL project and kept the interfaces there. Using Xamarin.Forms ver 1.2.2.6243.
2) Created an iOS class library (using classic API's) which has the implementation of above
interface.(references added for above)
3) Created an iOS project which is starting point and it has invoked the Dependency Service method
to get the object of implemented class. (references added for both of above)
4) Tried to get the object of above class and call some interface methods implemented in iOS library
project.
Results : I am getting runtime issue as Object returned every time by dependency services was null and not able to call any API’s implemented in iOS library.
Please suggest if we are missing any thing or done some thing wrong here.
put the attribute above namespace.
[assembly: Xamarin.Forms.Dependency(typeof (SQLite_iOS))]
namespace DataBinding.iOS
...
use the full name for 'Dependency', i.e. 'Xamarin.Forms.Dependency(...)'

DLL References into Application but not Class Library

I am trying to integrate the WeifenLuo.WinformsUI.Docking assembly into my application. To rule out external factors I created a new .Net 4 Winforms app and reference the DLL. Works fine.
I then created a .Net 4 Class Library and referenced the DLL. This this doesn't work and as soon as I try to use anything in that Docking namespace it won't compile.
To recap
C# Exe ---reference--> WeifenLuo.WinFormsUI.Docking.DockPanel.dll // OK
C# Class library ---reference--> WeifenLuo.WinFormsUI.Docking.DockPanel.dll // Not OK
I also have the WeifenLuo source and confirmed it's a class library referencing the same version of .Net. I tried adding a class library in their sample solution and referencing the WinForms project directly (not the result in assembly) and it still isn't linking properly.
Updating with Screenshot
You can't declare a private variable Inside a namespace.
Try the following code
namespace Test
{
public class Class1
{
private WeifenLuo.WinFormsUI.Docking.DockPanel dockPannel;
}
}
And if it still doesn't work, provide the error message.

Sharing assemblies using Portable Class Library with DataAnnotations

I have created a portable class library called DataContracts that contains my projects Messages and Views. Standard stuff like GetStockItemByIDRequest and StockView are contained in it.
The problem lies when I attempt to add DataAnnotations by using System.ComponentModel.DataAnnotations for some of my Views as such.
[DataContract]
public class StockView
{
[Required]
[DataMember]
public Guid StockID { get; set; }
[Required]
[DataMember]
public string Name { get; set; }
}
I can successfully add the System.ComponentModel.DataAnnotations to my Portable Class Library project and can successfully reference it in my Windows Phone 8 app and can even create a new instance of my view as such StockView View = new StockView(); within my Windows Phone App BUT if I try to use either Newtonsoft.Json or System.Net.Http.HttpClient by doing something like
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync("http://myservice.com");
T result = await response.Content.ReadAsAsync<T>();
OR
T result = await Newtonsoft.Json.JsonConvert.DeserializeObjectAsync<T>("{}");
ie: where deserialization is involved...
I am faced with the error Could not load file or assembly 'System.ComponentModel.DataAnnotations, Version=2.0.5.0'. Which I assume is because it doesn't appear that System.ComponentModel.DataAnnotations is supported in Windows Phone 8 (but then why can I add it as a reference to my PCL?).
So my questions are, why isn't this error invoked when I create a new instance of these classes directly and secondly how do I work around this?
Unfortunately DataAnnotations is not currently portable. While a bit complicated, you can probably work around that by writing your own DataAnnotation attributes in a PCL, and creating an assembly with the same name for .NET Framework projects which type-forwards the attributes to the "real" versions. See this answer for some more details on this.
OK, so it turns out that my original assumptions were completely wrong. You absolutely can reference the System.ComponentModel.DataAnnotations namespace from a Windows Phone 8 project.
Basically it comes down to counterintuatively referencing the silverlight version of the dll which can be located in C:\Program Files (x86)\Microsoft SDKs\Silverlight\v4.0\Libraries\Client\System.ComponentModel.DataAnnotations.dll
For more information about how to build portable class libraries I suggest referring to this article .
Data annotations is supported in certain PCL profiles.
Supported profiles:
.NET 4.0.3 and up
Windows Store 8 and up
Silverlight 4 and up
Most notably, the latest Windows Phone is not supported (8.1 at the time).
See full PCL feature table in:
http://msdn.microsoft.com/en-us/library/gg597391%28v=vs.110%29.aspx
1) The process of create a new class instance doesn't involve read custom attributes, that are loaded by reflection.
2) The System.ComponentModel.DataAnnotations is exclusive for ASP.NET
The System.ComponentModel.DataAnnotations namespace provides attribute
classes that are used to define metadata for ASP.NET MVC and ASP.NET
data controls.
The portable version of System.ComponentModel.DataAnnotations seems incomplete (eg no MaxLengthAttribute).
There is this library:
https://github.com/ryanhorath/PortableDataAnnotations:
Install-Package Portable.DataAnnotations
Your PCL needs to target Silverlight 8, otherwise you will get multiple class definition errors.

Categories

Resources