The type initializer for 'Tweetinvi.TwitterCredentials' threw an exception while Autofac is 3.3+ - exception

i want to retrieve tweets from twitter using specific hashtags.
i am using tweetinvi for this purpose.but i am getting an error
outer exception:
The type initializer for 'Tweetinvi.TwitterCredentials' threw an exception.
Inner exception:
{"Could not load file or assembly 'System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)":"System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes"}
i am using autofac 3.4.
kindly point out what am i doing wrong?
TwitterCredentials.SetCredentials("useraccesstoken",
"useraccessecret",
"consumerKey",
"consumerSecret");
var searchParameterSearch.GenerateTweetSearchParameter("#pakvsSA");
searchParameter.SetGeoCode(-122.398720, 37.781157, 1,DistanceMeasure.Miles);
searchParameter.Lang = Language.English;
searchParameter.SearchType = SearchResultType.Popular;
searchParameter.MaximumNumberOfResults = 100;
searchParameter.Until = new DateTime(2015, 12, 3);
searchParameter.SinceId = 399616835892781056;
searchParameter.MaxId = 405001488843284480;
var tweets = Search.SearchTweets(searchParameter);

So it looks like there are some .dll required by Tweetinvi (Sytem.Core) that are missing.
I would strongly advise using nuget to install Tweetinvi as a package as it will automatically install and reference all required dll.
https://www.nuget.org/packages/TweetinviAPI/

Related

How Can I Add The OutputStream Using Programmatical Configuration in Log4j2?

Any idea how I can add my output stream to the build config?
ConfigurationBuilder<BuiltConfiguration> builder =
ConfigurationBuilderFactory.newConfigurationBuilder();
AppenderComponentBuilder osAppender = builder.newAppender("os", "OutputStream");
osAppender.addAttribute("target", myStream);
builder.add(osAppender);
BuiltConfiguration config = builder.build();
Configurator.initialize(config);
This is the Error message I get:
2022-01-27 15:04:41,203 main ERROR OutputStream contains an invalid element or attribute "target"
2022-01-27 15:04:41,227 main ERROR Could not create plugin of type class org.apache.logging.log4j.core.appender.OutputStreamAppender for element OutputStream: java.lang.NullPointerException java.lang.NullPointerException
at org.apache.logging.log4j.core.appender.OutputStreamAppender.getManager(OutputStreamAppender.java:159)
Thanks
The ConfigurationBuilder API does not allow you to set attributes which can not be serialized to a String. Therefore you'll need to use OutputSreamAppender's builder directly:
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Configuration config = ctx.getConfiguration();
final Appender appender = OutputStreamAppender.newBuilder()//
.setTarget(myStream)
.setConfiguration(ctx.getConfiguration())
.build();
config.addLoggerAppender(ctx.getRootLogger(), appender);
See this question for another example of ConfigurationBuilder API vs direct instantiation of Log4j components.
Check also Log4j's architecture, which explains how all these components work together.

How to pass assembly reference to Razor.Parse()

I am using the RazerEngine at https://github.com/Antaris/RazorEngine
I am wondering how and if it is possible to pass an assembly reference to the engine?
Say I have some logic in an external DLL - how can I call a method in a custom DLL?
string template = "#using ClassLibrary1 #ClassLibrary1.Class1.SomethingFromADLL() ";
string result = Razor.Parse(template, m);
This results in an exception
Unable to compile template. The type or namespace name 'ClassLibrary1' could not be found (are you missing a using directive or an assembly reference?)
you have to find your necessary method and just write its name. For example:
String.Trim();
and
string template="String.Trim()";
I found that if I add the following line prior to the Razor.Parse method it now has access to the ClassLibrary1
Assembly a = Assembly.LoadWithPartialName("ClassLibrary1");
string template = "#using ClassLibrary1 #ClassLibrary1.Class1.SomethingFromADLL() ";
string result = Razor.Parse(template, m);

Stanford NLP ported to ikvm, FileLoad Exception

I'm trying to use the Stanford NLP tools ported to IKVM, but it gives me unhandeled exception.
here's the code I'm using
`string StanfordModelsDirectory = "englishPCFG.ser.gz";
try
{
LexicalizedParser LP = LexicalizedParser.loadModel(StanfordModelsDirectory);
TreebankLanguagePack tlp = new PennTreebankLanguagePack();
GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
Tree parse = LP.apply("what's the largest city in canada?");
parse.pennPrint();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}`
I've referenced IKVM.OpenJDK.Core and stanford-parser, but the message
"Could not load file or assembly 'IKVM.OpenJDK.Core, Version=7.1.4532.2, Culture=neutral, PublicKeyToken=13235d27fcbfff58' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)" appears.
I'm using windows 8 (visual studio 2012, .NET 4.5)
IKVM is compiled for .NET 2.0. You need to add a config file to your application to map to a different .NET version like 4.5.
IKVM 7.2.x contains such config file for ikvm.exe, ikvmc.exe, .....

Error in ServiceStack JSON processing under MonoDroid

I'm trying to use ServiceStack for Json serialization/deserialization in MonoDroid project.
I've built ServiceStack to run it in MonoDroid environment, but now I have issue with JSON deserialization.
Example code:
public class Track
{
public string Id { get; set; }
public string Title { get; set; }
public string Artist { get; set; }
public string Hash { get; set; }
public string Type { get; set; }
}
}
...
var track = new Track { Id = "1", Artist = "artist name", Hash = "654874", Title = "song title", Type = "mp3", };
var json = ServiceStack.Text.JsonSerializer.SerializeToString(track);
var track1 = ServiceStack.Text.JsonSerializer.DeserializeFromString<Track>(json);
On deserialization I have an exception:
Unhandled Exception:
System.ArgumentNullException: Argument cannot be null.
Parameter name: method
Stacktrace:
[External Code]
0x5E in ServiceStack.Text.Json.JsonReader.GetParseFn at [...]\Android\ServiceStack.Text\src\ServiceStack.Text\Json\JsonReader.Generic.cs:36 C#
0x2 in ServiceStack.Text.Json.JsonTypeSerializer.GetParseFn at [...]\Android\ServiceStack.Text\src\ServiceStack.Text\Json\JsonTypeSerializer.cs:283 C#
0xF in ServiceStack.Text.Common.TypeAccessor.Create at [...]\Android\ServiceStack.Text\src\ServiceStack.Text\Common\DeserializeType.cs:146 C#
0x142 in ServiceStack.Text.Common.DeserializeType`1[[ServiceStack.Text.Json.JsonTypeSerializer, ServiceStack.Text, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].GetParseMethod at [...]\Android\ServiceStack.Text\src\ServiceStack.Text\Common\DeserializeType.cs:60 C#
0x43A in ServiceStack.Text.Common.JsReader`1[[ServiceStack.Text.Json.JsonTypeSerializer, ServiceStack.Text, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].GetParseFn at [...]\Android\ServiceStack.Text\src\ServiceStack.Text\Common\JsReader.cs:100 C#
0x6 in ServiceStack.Text.Json.JsonReader`1[[Database.Models.Track, AssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]..cctor at [...]\Android\ServiceStack.Text\src\ServiceStack.Text\Json\JsonReader.Generic.cs:58 C#
0x1B in ServiceStack.Text.JsonSerializer.DeserializeFromString at [...]\Android\ServiceStack.Text\src\ServiceStack.Text\JsonSerializer.cs:33 C#
As I see there is:
var genericType = typeof(JsonReader<>).MakeGenericType(type);
var mi = genericType.GetMethod("GetParseFn", BindingFlags.Public | BindingFlags.Static);
parseFactoryFn = (ParseFactoryDelegate)Delegate.CreateDelegate(typeof(ParseFactoryDelegate), mi);
genericType is "ServiceStack.Text.Json.JsonReader`1[System.String]"
type is "System.String"
So, there "mi" variable is null
Are any ideas how to fix it, or where to look?
Thanks.
The problem here was that you had linking enabled in your build configuration, and the GetParseFn() method was being linked away, as it wasn't explicitly being referenced.
The linker is a static analysis tool that is included as part of Mono for Android's build process. It will scan through your compiled application and actually strip out any pieces of .NET (or in this case, your compiled ServiceStack class library) that aren't explicitly being referenced. This goes a long way to cut down the size of your application, but you can run into problems, as seen here. Since the GetParseFn() method was never being explicitly referenced (it is being called via reflection), the linker had no idea it was needed and linked it away.
There are several ways you can handle this problem going forward.
Disable linking entirely: you can turn off linking in the project's properties. In general it's easier to leave it off for debug builds, and turn it on for release builds. This obviously isn't a full solution if you ever plan on doing a release build, of course.
Link only SDK assemblies: this option will tell the linker only to operate on the .NET BCL and Android SDKs, and will leave your libraries alone.
Use PreserveAttribute: Using this attribute you can tell the linker to leave in certain pieces that you know are required, even though they're not explicitly referenced. Since ServiceStack is an external library and not your own, this probably isn't the best solution in this case.
Use Linkskip: This option allows you to specify specific assemblies that shouldn't be touched by the linker, allowing you to leave the linker on but simply opt out when needed.
Personally, in your situation here I could suggest going the Linkskip route. It would allow you to still get all the benefits of the linker, but have it ignore ServiceStack to avoid problems like this. In your project file, you would end up with something along the lines of this:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<AndroidLinkMode>Full</AndroidLinkMode>
<AndroidLinkSkip>ServiceStack.Text</AndroidLinkSkip>
</PropertyGroup>

Using DataContractJsonSerializer

I am trying to host a WCF service that responds to incoming requests by providing a json output stream. I have the following type
[DataContract]
[KnownType(typeof(List<HubCommon>))]
[KnownType(typeof(Music))]
[KnownType(typeof(AppsAndPlugins))]
[KnownType(typeof(Notifications))]
[KnownType(typeof(Scenes))]
[KnownType(typeof(Skins))]
[KnownType(typeof(Ringtones))]
[KnownType(typeof(Alarms))]
[KnownType(typeof(Widgets))]
[KnownType(typeof(Wallpapers))]
[KnownType(typeof(Soundsets))]
public class HubCommon{}
In my *.svc.cs file I do the following
List<HubCommon> hubContent = _ldapFacade.GetResults(query);
MemoryStream stream = new MemoryStream();
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(HubCommon));
serializer.WriteObject(stream,hubContent);
So essentially I am trying to serialize a List to Json but I get the following error on the "WriteObject" execution:-
The server encountered an error processing the request. The exception message is 'Type 'System.Collections.Generic.List`1[[HubContentCore.Domain.HubCommon, HubContentCore, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' with data contract name 'ArrayOfHubCommon:http://schemas.datacontract.org/2004/07/HubContentCore.Domain' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'
What am I missing here ?
Thanks in advance.
The type of your DataContractJsonSerializer is HubCommon but you are writing an object of type List<HubCommon> and HubCommon is not added to the KnownTypAttribute