MissingMethodException Plugin CRM2011 - c#

I found a couple of suggestions on this question but I can't get it on my problem.
We got a .ddl which replaces placeholders in a word file and returned a memorystream. This works fine in a deliverd Test Application with a WPF FrontEnd.
Now we need this solution in a CRM2011 context. I added a reference to this .dll file in my CRM Project, build the logic exactly the way as seen in the example and boom a MissingMethodException appears.
I debuged to the point where the Exception is thrown and found somethine like this:
readonly Dictionary<Type, object> typeMap = new Dictionary<Type, object>();
/// <summary>
/// Returns an instance of the DataService implementing the <typeparamref name="TService"/> interface
/// </summary>
/// <typeparam name="TService">type of the interface for the DataService</typeparam>
/// <returns></returns>
public TService For<TService>()
{
if (typeMap.ContainsKey(typeof(TService)))
{
object value = typeMap[typeof(TService)];
if (value is Type)
{
return (TService)Activator.CreateInstance((Type)typeMap[typeof(TService)]);
}
return (TService)value;
}
return Activator.CreateInstance<TService>();
}
The line Activator.CreateInstance(); throws the Exception. I have absolutely no idea what is going wrong here and why this piece of code works fine on the test app.

I'm assuming it's because you are either attempting to create a Static class, or a class without an empty constructor. Add try catch that displays the type that is attempting to be created to narrow down what is happening.

Related

'MvxWpfSetup<App>' must be a non-abstract type with a public parameterless constructor

I'm trying to follow the code at the end of this video here, but I'm getting this error around the 1:11:10 mark:
error CS0310: 'MvxWpfSetup<App>' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'TMvxSetup' in the generic type or method 'MvxSetupExtensions.RegisterSetupType<TMvxSetup>(object, params Assembly[])'
I really have no idea what code is relevant, but this is the file thats giving the error:
using MvvmCross.Core;
using MvvmCross.Platforms.Wpf.Core;
using MvvmCross.Platforms.Wpf.Views;
namespace MvxStarter.Wpf
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : MvxApplication
{
protected override void RegisterSetup()
{
this.RegisterSetupType<MvxWpfSetup<MvxStarter.Core.App>>();
}
}
}
I went over this whole section multiple times and I'm pretty certain I have exactly what he has. I even downloaded his source code, but I couldnt open the project so I copied and pasted all the code and I'm still getting this error. What should I do? I can post more relevant code if you tell me what to post. I have no idea what this error means and nothing I can find online about it makes any sense.
edit: I tried following the official documentation example project and I got the exact same error on the exact same line. Is something wrong with my installation?
https://www.mvvmcross.com/documentation/tutorials/tipcalc/the-core-project
https://www.mvvmcross.com/documentation/tutorials/tipcalc/a-wpf-ui-project
Need to create Setup class so code becomes.
using MvvmCross.Core;
using MvvmCross.Platforms.Wpf.Core;
using MvvmCross.Platforms.Wpf.Views;
namespace MvxStarter.Wpf
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : MvxApplication
{
protected override void RegisterSetup()
{
this.RegisterSetupType<Setup>();
}
}
}
Then Setup becomes
namespace MvxStarter.Wpf
{
public class Setup : MvxWpfSetup<Core.App>
{
protected override ILoggerProvider CreateLogProvider()
{
return new SerilogLoggerProvider();
}
protected override ILoggerFactory CreateLogFactory()
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.CreateLogger();
return new SerilogLoggerFactory();
}
}
}
or similar. This used Nuget Serilog as well as others.
I was running in to the same problem and found out it is due to breaking changes between MvvmCross 7 and MvvmCross8. In the video of Tim Corey version 7 is used.
Here is a link to the upgrade page: https://www.mvvmcross.com/documentation/upgrading/upgrade-to-mvvmcross-80
I want to add to ivie's answer that when using mvvmcross 8, you also need to add the attributes
[MvxContentPresentation]
[MvxViewFor(typeof(*YOURCLASSNAME*ViewModel))]
on top of the partial class in your corresponding view.
As shown on the TipsCalc example class on github
https://github.com/MvvmCross/MvvmCross-Samples/blob/master/TipCalc/TipCalc.WPF/Views/TipView.xaml.cs

RazorEngine: Razor.Parse throws FileNotFoundException every once in a while

I'm having an issue with RazorEngine (version 3.4.1.0).
I'm using Razor.Parse method with very simple template in a service that fires up every few minutes, and it works without any issues most of the times, but every now and then it throws this exception on me:
System.IO.FileNotFoundException:
Could not find file 'C:\Users\username\AppData\Local\Temp\cw3sv4yk.dll'.
File name: 'C:\Users\username\AppData\Local\Temp\cw3sv4yk.dll'
(cw3sv4yk is a randomly generated name)
Has anyone bumped into this issue before, and if so - any hints to what the solution would be?
Thanks,
Przemek
EDIT:
I've just noticed that I'm also getting this exception occasionally:
RazorEngine.Templating.TemplateCompilationException: Unable to compile
template. Metadata file
'c:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll'
could not be opened -- 'The process cannot access the file because it
is being used by another process.'\n\nOther compilation errors may
have occurred. Check the Errors property for more information.
EDIT_2:
One more exception that's being thrown every now and then:
System.ArgumentException: Class name is required
Using an overloaded version of Razor.Parse method which caches templates solved the issue for us:
/// <summary>
/// Parses and returns the result of the specified string template.
/// </summary>
/// <typeparam name="T">The model type.</typeparam>
/// <param name="razorTemplate">The string template.</param>
/// <param name="model">The model instance.</param>
/// <param name="cacheName">The name of the template type in the cache or NULL if no caching is desired.</param>
/// <returns>The string result of the template.</returns>
public static string Parse<T>(string razorTemplate, T model, string cacheName)

Accessing functions between two .csproj files in the same application

I incorporated a WLAN API given from Codeplex's Managed WiFi site into my C# project (Windows Form Application). In the API, different functions are given to retrieve various aspects of the machine's current WiFi profile. I am only interested in retrieving the RSSI strength given in the function below. I then want to take that value and stick it in a text box on my form.
(Visual Studio 2008)
In WlanAPI.cs file, the function I am interested in exists as such:
namespace NativeWifi
{
public class WlanClient
{
/// <summary>
/// Represents a Wifi network interface.
/// </summary>
public class WlanInterface
{
/// <summary>
/// Gets the RSSI.
/// </summary>
/// <value>The RSSI.</value>
/// <remarks>Not supported on Windows XP SP2.</remarks>
public int RSSI
{
get
{
return GetInterfaceInt(Wlan.WlanIntfOpcode.RSSI);
}
}
In myApp.cs I have a textbox simply named 'wifi' that will display the current RSSI.
I have included : 'using NativeWifi' in myApp.cs header, but can't seem to get the data from the RSSI function in the WlanAPI.csproj. The project builds and compiles just fine. I'm just snagged on getting the RSSI value.
In myApp.cs I have a statement to the effect of:
wifi.Text = (GetInterfaceInt(Wlan.WlanIntfOpcode.RSSI)); //app form txt_box=RSSI value
I know this is incorrect, but shows what I am trying to do.
Any ideas?
Thanks.
You should be able to solve the problems you are facing by
Adding a reference to WlanAPI.dll or WlanAPI project (if you add it to your solution)
Using code like following:
Using NativeWifi;
Class MyAPP : Form
{
public void PrintRSSI()
{
WlanClient client = new WlanClient();
var interfaces = client.Interfaces;
//Now chose an interface out of all the available interfaces. Usually there would be zero or 1 interfaces available
if(interfaces.Length > 0)
{
//Select first available interface. A more complicated logic can present the list of available interfaces to the user and and then display RSSI for the selected interface
wifi.Text = interfaces[0].RSSI.ToString();
}
}
//Other code for the class
}

SharedResourceDictionary: edit resource in Blend

To optimize my application, I create a SharedResourceDictionary. With this, I save about 250 mb of memory at run-time, so it works well.
My problem is in design-time, in Blend 4, I have no more access to my resource. To modify a template (witch is in my resource file), I usually right click on my control and I choose Edit Template --> Edit Current, like in this image:
I need to modify my template that way, it's 100x faster then to go in my resource file and find the good one... I have A LOT of resources...
My SharedResourceDictionary is implemented like this (found on the web):
public class SharedResourceDictionary : ResourceDictionary
{
/// <summary>
/// Internal cache of loaded dictionaries
/// </summary>
public static Dictionary<Uri, ResourceDictionary> _sharedDictionaries =
new Dictionary<Uri, ResourceDictionary>();
/// <summary>
/// Local member of the source uri
/// </summary>
private Uri _sourceUri;
/// <summary>
/// Gets or sets the uniform resource identifier (URI) to load resources from.
/// </summary>
public new Uri Source
{
get
{
if (IsInDesignMode)
return base.Source;
return _sourceUri;
}
set
{
_sourceUri = value;
if (!_sharedDictionaries.ContainsKey(value))
{
// If the dictionary is not yet loaded, load it by setting
// the source of the base class
base.Source = value;
// add it to the cache
_sharedDictionaries.Add(value, this);
}
else
{
// If the dictionary is already loaded, get it from the cache
MergedDictionaries.Add(_sharedDictionaries[value]);
}
}
}
/// <summary>
/// Check if we are in Blend to prevent error
/// </summary>
public bool IsInDesignMode
{
get
{
return
(bool)
DependencyPropertyDescriptor.FromProperty(DesignerProperties.IsInDesignModeProperty,
typeof(DependencyObject)).Metadata.DefaultValue;
}
}
}
Someone have an idea if there is a solution for this? I really want to keep this shared dictionary due to the memory improvement, but I also want to modify my resource easily...
Any clue will be appreciated. Thank you.
EDIT:
Doing this (SharedResourceDictionary), I also have an issue with static resource:
Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.
And when I change them to dynamic resource it works, or if I change the SharedResourceDictionary by a simple ResourceDictionary it also works. The project can compile but I cannot edit the resource without a modification as I said.
Are you compiling for .NET 4?
You might be hitting the bug where it fails to scan your ResourceDictionaries properly.
There's talk that you have to add your ResourceDictionaries at the the App.xaml level (in Application.Resources) level, and add a dummy default style.
This might not work for you as you seem to be adding your resources in your control.
http://blogs.windowsclient.net/rob_relyea/archive/2010/04/26/my-staticresource-reference-worked-differently-with-wpf-4-rc-than-it-does-with-wpf-4-rtm.aspx
Adding a Merged Dictionary to a Merged Dictionary
Trouble referencing a Resource Dictionary that contains a Merged Dictionary
http://connect.microsoft.com/VisualStudio/feedback/details/553528/defaultstylekey-style-not-found-in-inner-mergeddictionaries
Actually it is problematic to use this feature. =(
Maybe in the future things will get better, do not know if will help, but you can try a different implementation:
WPF SharedResourceDictionary
(but do not guarantee it will work)
I gave up using the SharedResourceDictionary because of problems at design time, only when the project is 100% completed I'll be back to use.

How to convert IPictureDisp to System.Drawing.Image

From a COM library (Microsoft Office Document Imaging aka MODI) I receive an image as an IPictureDisp which I would like to convert to a System.Drawing.Image object.
What would be the best way to do that?
Currently I'm using the code below, which however throws an NotImplementedException.
internal sealed class IPictureDispHost : AxHost
{
/// <summary>
/// Default Constructor, required by the framework.
/// </summary>
private IPictureDispHost() : base(string.Empty) { }
/// <summary>
/// Convert the image to an ipicturedisp.
/// </summary>
/// <param name="image">The image instance</param>
/// <returns>The picture dispatch object.</returns>
public new static object GetIPictureDispFromPicture(Image image)
{
return AxHost.GetIPictureDispFromPicture(image);
}
/// <summary>
/// Convert the dispatch interface into an image object.
/// </summary>
/// <param name="picture">The picture interface</param>
/// <returns>An image instance.</returns>
public new static Image GetPictureFromIPicture(object picture)
{
return AxHost.GetPictureFromIPicture(picture);
}
}
...
// somewhere later the conversion gets called
Image image = IPictureDispHost.GetPictureFromIPicture(picture);
This is the exception stack trace:
System.NotImplementedException: The method or operation is not implemented.
at System.Windows.Forms.UnsafeNativeMethods.IPicture.GetHandle()
at System.Windows.Forms.AxHost.GetPictureFromIPicture(Object picture)
at DocumentViewer.IPictureDispHost.GetPictureFromIPicture(Object picture)
I have references to stdole, System.Windows.Forms and System.Drawing in my project. Am I missing something?
Check out this article.
It describes three different options to take, just pick the one you find easiest or "cleanest" for your purposes (including the one you claim not to be working for you).
Olivier Jacot-Descombes: The link above is broken. I've added the corresponding link from the Internet Archive WayBackMachine:
Converting between IPictureDisp and System.Drawing.Image (MSDN Blogs > Andrew Whitechapel).
As it seems the picture obtained from the Microsoft Office Document Imaging COM components is not a valid IPictureDisp object and there seems no way to convert it.
Function GetImage(MyIPicture As stdole.IPictureDisp) As Drawing.Image
If CType(MyIPicture.Type, Integer) <> 1 then Throw New ArgumentException("Image not supported")
Return Drawing.Image.FromHbitmap(MyIPicture.Handle, MyIPicture.hPal)
End Function
Inspired from this post in the MSDN Forum.
No idea why the CType(MyIPicture.Type, Integer) = 1 is required, but it works..
Have you tried:
picture1.image = Microsoft.VisualBasic.Compatibility.VB6.Support.IPictureDispToImage(pict)
Microsoft.VisualBasic.Compatibility.VB6.Support.IPictureDispToImage is not guaranteed to always be included in future updates. So... taking the example from http://support.microsoft.com/kb/555417 I came up with the following
Example
Public Class ImageToPictureDispConverter
Inherits System.Windows.Forms.AxHost
Public Sub New()
MyBase.New("{63109182-966B-4e3c-A8B2-8BC4A88D221C}")
End Sub
Public Function GetImageFromIPictureDisp(ByVal objImage As stdole.IPictureDisp) As System.Drawing.Image
Dim objPicture As System.Drawing.Image
objPicture = CType(MyBase.GetPictureFromIPicture(objImage), System.Drawing.Image)
Return objPicture
End Function
End Class
I see in your constructor that you pass an empty string. I ended up having to pass the following string "{63109182-966B-4e3c-A8B2-8BC4A88D221C}". If I passed an empty string I received a system.formatexception error. It looks like you have everything I have except for this string in your call to base.
Hope this helps.

Categories

Resources